Implement /find_def_locations in IDE Service
This commit is contained in:
parent
14cb991122
commit
ed8a3779d3
65
src/ide_services/endpoints/findDefs.ts
Normal file
65
src/ide_services/endpoints/findDefs.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import * as vscode from "vscode";
|
||||
|
||||
import { IDEService } from "../types";
|
||||
import { assert } from "console";
|
||||
|
||||
/**
|
||||
* Find definition locations for a symbol
|
||||
*
|
||||
* @param abspath: absolute path of the file
|
||||
* @param line: line number of the symbol, 0-based
|
||||
* @param character: character number, 0-based
|
||||
*/
|
||||
export async function findDefinitionLocations(
|
||||
abspath: string,
|
||||
line: string,
|
||||
character: string
|
||||
): Promise<IDEService.Location[]> {
|
||||
const ln = Number(line);
|
||||
const col = Number(character);
|
||||
assert(!isNaN(ln) && !isNaN(col), "line and character must be numbers");
|
||||
|
||||
const position = new vscode.Position(ln, col);
|
||||
const uri = vscode.Uri.file(abspath);
|
||||
|
||||
const defs = await vscode.commands.executeCommand<
|
||||
vscode.Location[] | vscode.LocationLink[]
|
||||
>("vscode.executeDefinitionProvider", uri, position);
|
||||
|
||||
const locations: IDEService.Location[] = [];
|
||||
defs.forEach((def) => {
|
||||
locations.push(toLocation(def));
|
||||
});
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param location
|
||||
* @returns
|
||||
*/
|
||||
function toLocation(
|
||||
location: vscode.Location | vscode.LocationLink
|
||||
): IDEService.Location {
|
||||
const range = "range" in location ? location.range : location.targetRange;
|
||||
const uri = "uri" in location ? location.uri : location.targetUri;
|
||||
const start = range.start;
|
||||
const end = range.end;
|
||||
|
||||
const loc: IDEService.Location = {
|
||||
abspath: uri.fsPath,
|
||||
range: {
|
||||
start: {
|
||||
line: start.line,
|
||||
character: start.character,
|
||||
},
|
||||
end: {
|
||||
line: end.line,
|
||||
character: end.character,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return loc;
|
||||
}
|
@ -12,6 +12,7 @@ import { LegacyEndpoints } from "./endpoints/legacy";
|
||||
import { UnofficialEndpoints } from "./endpoints/unofficial";
|
||||
import { getDocumentSymbols } from "./endpoints/getDocumentSymbols";
|
||||
import { findTypeDefinitionLocations } from "./endpoints/findTypeDefs";
|
||||
import { findDefinitionLocations } from "./endpoints/findDefs";
|
||||
|
||||
const functionRegistry: any = {
|
||||
/**
|
||||
@ -41,6 +42,10 @@ const functionRegistry: any = {
|
||||
keys: ["abspath"],
|
||||
handler: getDocumentSymbols,
|
||||
},
|
||||
"/find_def_locations": {
|
||||
keys: ["abspath", "line", "character"],
|
||||
handler: findDefinitionLocations,
|
||||
},
|
||||
"/find_type_def_locations": {
|
||||
keys: ["abspath", "line", "character"],
|
||||
handler: findTypeDefinitionLocations,
|
||||
|
Loading…
x
Reference in New Issue
Block a user