-
Notifications
You must be signed in to change notification settings - Fork 55
Add a "go to implementation" action for scopes #462
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
Open
N1ebieski
wants to merge
6
commits into
laravel:main
Choose a base branch
from
N1ebieski:Add-a-Go-to-implementation-action-for-scopes-#59
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
676fed7
Add a "Go to implementation" action for scopes
N1ebieski c549104
isInHoverRange checks if one range contains another one
N1ebieski bfd2341
refactoring
N1ebieski 76f1896
replace uri with $method->getFileName() with LaravelVsCode::relativePath
N1ebieski f3c7d6e
replace uri $method->getFileName() with LaravelVsCode::relativePath
N1ebieski d4b29f5
increase font
N1ebieski 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,85 @@ | ||
import { HoverActions } from "@src/hoverAction/support"; | ||
import { getModelByClassname } from "@src/repositories/models"; | ||
import { detect } from "@src/support/parser"; | ||
import { projectPath } from "@src/support/project"; | ||
import { AutocompleteParsingResult } from "@src/types"; | ||
import * as vscode from "vscode"; | ||
|
||
const isInHoverRange = ( | ||
range: vscode.Range, | ||
method: AutocompleteParsingResult.MethodCall, | ||
): boolean => { | ||
if (!method.start || !method.end) { | ||
return false; | ||
} | ||
|
||
const nodeRange = new vscode.Range( | ||
new vscode.Position(method.start.line, method.start.column), | ||
new vscode.Position(method.end.line, method.end.column), | ||
); | ||
|
||
// vs-code-php-parser-cli returns us the position of the entire node, for example: | ||
// | ||
// Example::popular() | ||
// ->traitScope() | ||
// ->active(); | ||
// | ||
// so we don't have to check equality of the start and end positions. | ||
// It's enough to check if the node range contains the scope range | ||
return nodeRange.contains(range); | ||
}; | ||
|
||
export class ScopeHoverProvider implements vscode.HoverProvider { | ||
provideHover( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
): vscode.ProviderResult<vscode.Hover> { | ||
const range = document.getWordRangeAtPosition(position); | ||
|
||
if (!range) { | ||
return null; | ||
} | ||
|
||
const scopeName = document.getText(range); | ||
|
||
return detect(document).then((results) => { | ||
if (!results) { | ||
return null; | ||
} | ||
|
||
const result = results | ||
.filter((result) => result.type === "methodCall") | ||
.find( | ||
(result) => | ||
result.methodName === scopeName && | ||
isInHoverRange(range, result), | ||
); | ||
|
||
if (!result?.className) { | ||
return null; | ||
} | ||
|
||
const scope = getModelByClassname(result.className)?.scopes?.find( | ||
(scope) => scope.name === scopeName, | ||
); | ||
|
||
if (!scope?.path) { | ||
return null; | ||
} | ||
|
||
const hoverActions = new HoverActions([ | ||
{ | ||
title: "Go to implementation", | ||
command: "laravel.open", | ||
arguments: [ | ||
vscode.Uri.file(projectPath(scope.path)), | ||
scope.start_line, | ||
0, | ||
], | ||
}, | ||
]); | ||
|
||
return new vscode.Hover(hoverActions.getAsMarkdownString(), range); | ||
}); | ||
} | ||
} |
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,30 @@ | ||
import * as vscode from "vscode"; | ||
|
||
export class HoverActions { | ||
private readonly commands: vscode.Command[]; | ||
|
||
constructor(commands: vscode.Command[] = []) { | ||
this.commands = commands; | ||
} | ||
|
||
public push(command: vscode.Command): this { | ||
this.commands.push(command); | ||
|
||
return this; | ||
} | ||
|
||
public getAsMarkdownString(): vscode.MarkdownString { | ||
let string = ""; | ||
|
||
this.commands.forEach((command) => { | ||
string += `[${command.title}](command:${command.command}?${encodeURIComponent(JSON.stringify(command.arguments))})`; | ||
string += `<span style="display: none;"> </span>`; | ||
}); | ||
|
||
const markdownString = new vscode.MarkdownString(string); | ||
markdownString.supportHtml = true; | ||
markdownString.isTrusted = true; | ||
|
||
return markdownString; | ||
} | ||
} |
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
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
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.