Skip to content

Commit

Permalink
Fixed hover and go to definition not working when cursor is right at …
Browse files Browse the repository at this point in the history
…start of the function name / keyword
  • Loading branch information
eyza-cod2 committed Nov 4, 2024
1 parent 0524f63 commit b3d7968
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/GscDefinitionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class GscDefinitionProvider implements vscode.DefinitionProvider {
const gscData = gscFile.data;

// Get group before cursor
var groupAtCursor = gscData.root.findGroupOnLeftAtPosition(position);
var groupAtCursor = gscData.root.findKeywordAtPosition(position);
if (groupAtCursor === undefined || groupAtCursor.parent === undefined) {
return locations;
}
Expand Down
46 changes: 46 additions & 0 deletions src/GscFileParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,52 @@ export class GscGroup {
}




public findKeywordAtPosition(position: vscode.Position, token?: GscToken): GscGroup | undefined {

if (token === undefined) {
// Loop all tokens
for (const t of this.tokensAll) {
const t2 = this.tokensAll.at(t.index + 1);

if (position.isBefore(t.range.start)) {
return undefined;
}

// Next is keyword, make this range check smaller
if (t2?.type === TokenType.Keyword) {
if (position.isAfterOrEqual(t.range.start) && position.isBefore(t.range.end)) {
token = t;
break;
}
} else {
if (position.isAfterOrEqual(t.range.start) && position.isBeforeOrEqual(t.range.end)) {
token = t;
break;
}
}

}
}
if (token === undefined) {
return undefined;
}

// Find the group owning the token
for (const group of this.items) {
if (token.index >= group.tokenIndexStart && token.index <= group.tokenIndexEnd) {
const group2 = group.findKeywordAtPosition(position);
if (group2 === undefined) {
return group;
}
return group2;
}
}
}



/**
* Find parent group of current group that contains given range.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/GscHoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class GscHoverProvider implements vscode.HoverProvider {
const errorDiagnosticsDisabled = gscFile.config.errorDiagnostics === ConfigErrorDiagnostics.Disable;

// Get group before cursor
var groupAtCursor = gscData.root.findGroupOnLeftAtPosition(position);
var groupAtCursor = gscData.root.findKeywordAtPosition(position);

if (groupAtCursor?.type === GroupType.FunctionName) {
const funcInfo = groupAtCursor.getFunctionReferenceInfo();
Expand Down
2 changes: 1 addition & 1 deletion src/GscReferenceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class GscReferenceProvider implements vscode.ReferenceProvider {
const gscData = gscFile.data;

// Get group before cursor
var groupAtCursor = gscData.root.findGroupOnLeftAtPosition(position);
var groupAtCursor = gscData.root.findKeywordAtPosition(position);
if (groupAtCursor === undefined || groupAtCursor.parent === undefined) {
return locations;
}
Expand Down

0 comments on commit b3d7968

Please sign in to comment.