-
Notifications
You must be signed in to change notification settings - Fork 52
Show inline values #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
paulacamargo25
merged 9 commits into
microsoft:main
from
paulacamargo25:Show-inline-values
Jul 19, 2024
Merged
Show inline values #384
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c38df6e
Add inline provider
paulacamargo25 ea33e67
add inlineProvider
paulacamargo25 5631e6f
Add inlie provider
paulacamargo25 23739bb
Add tests
paulacamargo25 4519e45
fix import
paulacamargo25 7a7bd62
fix lint
paulacamargo25 c9ed0cd
Update inline function to remove strins values
paulacamargo25 43eac63
Add unit test for class type
paulacamargo25 967767a
fix merge
paulacamargo25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/extension/debugger/inlineValue/pythonInlineValueProvider.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import { | ||
InlineValue, | ||
InlineValueContext, | ||
InlineValuesProvider, | ||
Range, | ||
TextDocument, | ||
InlineValueVariableLookup, | ||
InlineValueEvaluatableExpression, | ||
} from 'vscode'; | ||
import { customRequest } from '../../common/vscodeapi'; | ||
|
||
export class PythonInlineValueProvider implements InlineValuesProvider { | ||
public async provideInlineValues( | ||
document: TextDocument, | ||
viewPort: Range, | ||
context: InlineValueContext, | ||
): Promise<InlineValue[]> { | ||
let scopesRequest = await customRequest('scopes', { frameId: context.frameId }); | ||
let variablesRequest = await customRequest('variables', { | ||
variablesReference: scopesRequest.scopes[0].variablesReference, | ||
}); | ||
|
||
//https://docs.python.org/3/reference/lexical_analysis.html#keywords | ||
const pythonKeywords = [ | ||
'False', | ||
'await', | ||
'else', | ||
'import ', | ||
'pass', | ||
'None', | ||
'break', | ||
'except', | ||
'in', | ||
'raise', | ||
'True', | ||
'class', | ||
'finally', | ||
'is', | ||
'return', | ||
'and', | ||
'continue', | ||
'for', | ||
'lambda', | ||
'try', | ||
'as', | ||
'def', | ||
'from', | ||
'nonlocal', | ||
'while', | ||
'assert', | ||
'del', | ||
'global', | ||
'not', | ||
'with', | ||
'async', | ||
'elif', | ||
'if', | ||
'or', | ||
'yield', | ||
'self', | ||
]; | ||
|
||
const pythonVariables: any[] = variablesRequest.variables | ||
.filter((variable: any) => variable.type) | ||
.map((variable: any) => variable.name); | ||
|
||
let variableRegex = new RegExp( | ||
'(?:self.)?' + //match self. if present | ||
'[a-zA-Z_][a-zA-Z0-9_]*', //math variable name | ||
'g', | ||
); | ||
|
||
const allValues: InlineValue[] = []; | ||
for (let l = viewPort.start.line; l <= viewPort.end.line; l++) { | ||
const line = document.lineAt(l); | ||
// Skip comments | ||
if (line.text.trimStart().startsWith('#')) { | ||
continue; | ||
} | ||
|
||
let code = removeCharsOutsideBraces(line.text); | ||
|
||
for (let match = variableRegex.exec(code); match; match = variableRegex.exec(code)) { | ||
let varName = match[0]; | ||
// Skip python keywords | ||
if (pythonKeywords.includes(varName)) { | ||
continue; | ||
} | ||
if (pythonVariables.includes(varName.split('.')[0])) { | ||
if (varName.includes('self')) { | ||
const rng = new Range(l, match.index, l, match.index + varName.length); | ||
allValues.push(new InlineValueEvaluatableExpression(rng, varName)); | ||
} else { | ||
const rng = new Range(l, match.index, l, match.index + varName.length); | ||
allValues.push(new InlineValueVariableLookup(rng, varName, false)); | ||
} | ||
} | ||
} | ||
} | ||
return allValues; | ||
} | ||
} | ||
|
||
function removeCharsOutsideBraces(code: string): string { | ||
// Regular expression to find Python strings | ||
const stringRegex = /(["'])(?:(?=(\\?))\2.)*?\1/g; | ||
|
||
//Regular expression to match values inside {} | ||
const insideBracesRegex = /{[^{}]*}/g; | ||
|
||
return code.replace(stringRegex, (match) => { | ||
const content = match.slice(1, -1); | ||
|
||
let result = ''; | ||
let tempMatch; | ||
|
||
while ((tempMatch = insideBracesRegex.exec(content)) !== null) { | ||
result += tempMatch[0]; | ||
} | ||
const processedContent = result || content; | ||
|
||
return match[0] + processedContent + match[0]; | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Person: | ||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age | ||
|
||
def greet(self): | ||
return f"Hello, my name is {self.name} and I a {self.age} years old." | ||
|
||
person1 = Person("John Doe", 30) | ||
person1.greet() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var1 = 5 | ||
var2 = 7 | ||
var3 = "hola" | ||
var4 = {"a": 1, "b": 2} | ||
var5 = [1, 2, 3] | ||
var6 =var1 + var2 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens for cases like
self.var1
? Or cases like[var1, var2]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cases of variables inside other variables works, and now I updated the code in order to work with self.