-
Notifications
You must be signed in to change notification settings - Fork 226
Add code lens for quick evaluation #1035
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
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b9c5e38
Add code lens for quick eval command
marcnjaramillo 5d40748
Ensure commented out predicates do not have code lens
marcnjaramillo 0a27a72
Improve conditional check for commented out predicate detection
marcnjaramillo 452ad4d
Refactor regex
marcnjaramillo a39e47f
Merge branch 'main' into codelens-quick-eval
marcnjaramillo 806e594
Move comment check to eliminate evaluating regex more than once
marcnjaramillo c429c9a
Merge branch 'main' of github.com:marcnjaramillo/vscode-codeql into c…
marcnjaramillo 153a378
Merge branch 'codelens-quick-eval' of github.com:marcnjaramillo/vscod…
marcnjaramillo 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
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,39 @@ | ||
| import { | ||
| CodeLensProvider, | ||
| TextDocument, | ||
| CodeLens, | ||
| Command, | ||
| Range | ||
| } from 'vscode'; | ||
|
|
||
| class QuickEvalCodeLensProvider implements CodeLensProvider { | ||
| async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> { | ||
|
|
||
| const codeLenses: CodeLens[] = []; | ||
|
|
||
| for (let index = 0; index < document.lineCount; index++) { | ||
| const textLine = document.lineAt(index); | ||
| // Match a predicate signature, including predicate name, parameter list, and opening brace. | ||
| const regex = new RegExp(/(\w+)\s*\(\s*.*(?:,\s*)*\)\s*\{/); | ||
| const matches = textLine.text.match(regex); | ||
|
|
||
| if (matches) { | ||
| const range: Range = new Range( | ||
| textLine.range.start.line, matches.index!, | ||
| textLine.range.end.line, matches.index! + 1 | ||
| ); | ||
|
|
||
| const command: Command = { | ||
| command: 'codeQL.codeLensQuickEval', | ||
| title: `Quick Evaluation: ${matches[1]}`, | ||
| arguments: [document.uri, range] | ||
| }; | ||
| const codeLens = new CodeLens(range, command); | ||
| codeLenses.push(codeLens); | ||
| } | ||
| } | ||
| return codeLenses; | ||
| } | ||
| } | ||
|
|
||
| export default QuickEvalCodeLensProvider; | ||
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
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.
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.
Ensure that commented code removes the code lens. It's too tricky to do in a single regex, so I think doing another check is ok.
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.
We had an illuminating discussion offline about why this works (
searchreturns 0 when it matches the start of the line and -1 on mismatch, which JS converts intofalseandtruerespectively). I suggested a slightly more verbose but arguably simpler use oftestinstead -- hopefully that's still captured the meaning you suggested here!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.
A downside with the suggestion is that the regex is evaluated twice for every line, even if there is no initial match. The original suggestion only ran the second regex if the first one matched. I'm not sure if the extra overhead would be noticeable, but this code is run after every file change (in a background thread).
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.
That's a good point. We can keep using
!testrather thansearch, but keep it inline instead of in its own variable.So
if (matches && /.../.test(textLine.text)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.
Yep....that makes sense.
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.
Made this change. Sorry it took a bit. I forgot I had resolved the
CHANGELOGconflict from here but I hadn't updated my local branch yet. Had to get that squared away. Does it look better?