|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { |
| 5 | + InlineValue, |
| 6 | + InlineValueContext, |
| 7 | + InlineValuesProvider, |
| 8 | + Range, |
| 9 | + TextDocument, |
| 10 | + InlineValueVariableLookup, |
| 11 | + InlineValueEvaluatableExpression, |
| 12 | +} from 'vscode'; |
| 13 | +import { customRequest } from '../../common/vscodeapi'; |
| 14 | + |
| 15 | +export class PythonInlineValueProvider implements InlineValuesProvider { |
| 16 | + public async provideInlineValues( |
| 17 | + document: TextDocument, |
| 18 | + viewPort: Range, |
| 19 | + context: InlineValueContext, |
| 20 | + ): Promise<InlineValue[]> { |
| 21 | + let scopesRequest = await customRequest('scopes', { frameId: context.frameId }); |
| 22 | + let variablesRequest = await customRequest('variables', { |
| 23 | + variablesReference: scopesRequest.scopes[0].variablesReference, |
| 24 | + }); |
| 25 | + |
| 26 | + //https://docs.python.org/3/reference/lexical_analysis.html#keywords |
| 27 | + const pythonKeywords = [ |
| 28 | + 'False', |
| 29 | + 'await', |
| 30 | + 'else', |
| 31 | + 'import ', |
| 32 | + 'pass', |
| 33 | + 'None', |
| 34 | + 'break', |
| 35 | + 'except', |
| 36 | + 'in', |
| 37 | + 'raise', |
| 38 | + 'True', |
| 39 | + 'class', |
| 40 | + 'finally', |
| 41 | + 'is', |
| 42 | + 'return', |
| 43 | + 'and', |
| 44 | + 'continue', |
| 45 | + 'for', |
| 46 | + 'lambda', |
| 47 | + 'try', |
| 48 | + 'as', |
| 49 | + 'def', |
| 50 | + 'from', |
| 51 | + 'nonlocal', |
| 52 | + 'while', |
| 53 | + 'assert', |
| 54 | + 'del', |
| 55 | + 'global', |
| 56 | + 'not', |
| 57 | + 'with', |
| 58 | + 'async', |
| 59 | + 'elif', |
| 60 | + 'if', |
| 61 | + 'or', |
| 62 | + 'yield', |
| 63 | + 'self', |
| 64 | + ]; |
| 65 | + |
| 66 | + const pythonVariables: any[] = variablesRequest.variables |
| 67 | + .filter((variable: any) => variable.type) |
| 68 | + .map((variable: any) => variable.name); |
| 69 | + |
| 70 | + let variableRegex = new RegExp( |
| 71 | + '(?:self.)?' + //match self. if present |
| 72 | + '[a-zA-Z_][a-zA-Z0-9_]*', //math variable name |
| 73 | + 'g', |
| 74 | + ); |
| 75 | + |
| 76 | + const allValues: InlineValue[] = []; |
| 77 | + for (let l = viewPort.start.line; l <= viewPort.end.line; l++) { |
| 78 | + const line = document.lineAt(l); |
| 79 | + // Skip comments |
| 80 | + if (line.text.trimStart().startsWith('#')) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + let code = removeCharsOutsideBraces(line.text); |
| 85 | + |
| 86 | + for (let match = variableRegex.exec(code); match; match = variableRegex.exec(code)) { |
| 87 | + let varName = match[0]; |
| 88 | + // Skip python keywords |
| 89 | + if (pythonKeywords.includes(varName)) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + if (pythonVariables.includes(varName.split('.')[0])) { |
| 93 | + if (varName.includes('self')) { |
| 94 | + const rng = new Range(l, match.index, l, match.index + varName.length); |
| 95 | + allValues.push(new InlineValueEvaluatableExpression(rng, varName)); |
| 96 | + } else { |
| 97 | + const rng = new Range(l, match.index, l, match.index + varName.length); |
| 98 | + allValues.push(new InlineValueVariableLookup(rng, varName, false)); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + return allValues; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +function removeCharsOutsideBraces(code: string): string { |
| 108 | + // Regular expression to find Python strings |
| 109 | + const stringRegex = /(["'])(?:(?=(\\?))\2.)*?\1/g; |
| 110 | + |
| 111 | + //Regular expression to match values inside {} |
| 112 | + const insideBracesRegex = /{[^{}]*}/g; |
| 113 | + |
| 114 | + return code.replace(stringRegex, (match) => { |
| 115 | + const content = match.slice(1, -1); |
| 116 | + |
| 117 | + let result = ''; |
| 118 | + let tempMatch; |
| 119 | + |
| 120 | + while ((tempMatch = insideBracesRegex.exec(content)) !== null) { |
| 121 | + result += tempMatch[0]; |
| 122 | + } |
| 123 | + const processedContent = result || content; |
| 124 | + |
| 125 | + return match[0] + processedContent + match[0]; |
| 126 | + }); |
| 127 | +} |
0 commit comments