Skip to content

Commit

Permalink
spanning range
Browse files Browse the repository at this point in the history
  • Loading branch information
yonip23 committed Aug 13, 2023
1 parent 65b0af1 commit ad04736
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
47 changes: 47 additions & 0 deletions src/binary/requests/spanningRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as vscode from "vscode";
import { Logger } from "../../utils/logger";
import { tabNineProcess } from "./requests";

export type Position = {
line: number;
column: number;
};

export type SpanningRangeRequest = {
file: string;
position: Position;
}

export type Range = {
start: Position;
end: Position;
}

export type SpanningRangeResponse = {
type: "Success" | "Error";
range?: Range;
message?: string;
};

export async function getSpanningRange(request: SpanningRangeRequest): Promise<vscode.Range | undefined> {
const response = await tabNineProcess.request({
SpanningRange: request
}) as SpanningRangeResponse;

if (response.type === "Error") {
Logger.error(`failed to get spanning range: ${response.message || 'unknown error'}`);
return undefined;
}

if (!response.range) return undefined;

return toVscodeRange(response.range);
}

function toVscodeRange(range: Range): vscode.Range {
return new vscode.Range(toVscodePosition(range.start), toVscodePosition(range.end));
}

function toVscodePosition(position: Position): vscode.Position {
return new vscode.Position(position.line, position.column);
}
35 changes: 12 additions & 23 deletions src/tabnineChatWidget/handlers/resolveSymbols.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from "vscode";
import * as path from "path";
import { Logger } from "../../utils/logger";
import { getSpanningRange } from "../../binary/requests/spanningRange";

export type SymbolInformationResult = vscode.SymbolInformation & {
relativePath: string;
Expand All @@ -9,10 +10,8 @@ export type SymbolInformationResult = vscode.SymbolInformation & {

export async function resolveSymbols({
symbol,
document,
}: {
symbol: string;
document: vscode.TextDocument;
}): Promise<SymbolInformationResult[] | undefined> {
const symbols = await vscode.commands.executeCommand<
vscode.SymbolInformation[]
Expand All @@ -37,7 +36,7 @@ export async function resolveSymbols({
if (!relativePath || isProbablyNotSource(relativePath)) {
return undefined;
}
const symbolText = await findSymbolText(workspaceSymbol, document);
const symbolText = await findSymbolText(workspaceSymbol);
if (!symbolText) {
Logger.warn("Failed to find symbol text");
return undefined;
Expand All @@ -53,31 +52,21 @@ export async function resolveSymbols({
)) as SymbolInformationResult[];
}

async function findSymbolText(
workspaceSymbol: vscode.SymbolInformation,
document: vscode.TextDocument
) {
async function findSymbolText(workspaceSymbol: vscode.SymbolInformation): Promise<string | undefined> {
const symbolDocument = await vscode.workspace.openTextDocument(
workspaceSymbol.location.uri
);
const docFoldingRanges = await vscode.commands.executeCommand<
vscode.FoldingRange[]
>("vscode.executeFoldingRangeProvider", workspaceSymbol.location.uri);
const symbolFoldingRange = docFoldingRanges?.find(
(range) => range.start === workspaceSymbol.location.range.start.line
);
const symbolText = symbolFoldingRange
? symbolDocument.getText(rangeFor(symbolFoldingRange))
: undefined;

return symbolText;
}
const range = await getSpanningRange({
file: workspaceSymbol.location.uri.fsPath,
position: {
line: workspaceSymbol.location.range.start.line,
column: workspaceSymbol.location.range.start.character,
}
});

function rangeFor(foldingRange: vscode.FoldingRange): vscode.Range {
return new vscode.Range(
new vscode.Position(foldingRange.start, 0),
new vscode.Position(foldingRange.end, 0)
);
const symbolText = range && symbolDocument.getText(range);
return symbolText;
}

function isProbablyNotSource(symbolPath: string): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ export default async function findSymbolsCommandExecutor(
const snakeCaseArg = toSnakeCase(arg);
const camelCaseSymbols = resolveSymbols({
symbol: camelCaseArg,
document: editor.document,
});
}).then((result) => result?.slice(0, 3));
const snakeCaseSymbols = resolveSymbols({
symbol: snakeCaseArg,
document: editor.document,
});
}).then((result) => result?.slice(0, 3));

const allSymbols = (
await Promise.all([camelCaseSymbols, snakeCaseSymbols])
).reduce((acc, val) => (acc || []).concat(val || []), []);

return allSymbols?.map((symbol) =>
constructTextForSymbol(symbol, language.toLowerCase())
);
return allSymbols
?.filter((symbol) => !!symbol)
.map((symbol) => constructTextForSymbol(symbol, language.toLowerCase()));
}

function constructTextForSymbol(
Expand Down

0 comments on commit ad04736

Please sign in to comment.