139 lines
3.6 KiB
TypeScript
139 lines
3.6 KiB
TypeScript
import { IDE } from "../../index";
|
|
import { findUriInDirs } from "../../util/uri";
|
|
import { ContextRetrievalService } from "../context/ContextRetrievalService";
|
|
import { GetLspDefinitionsFunction } from "../types";
|
|
import { HelperVars } from "../util/HelperVars";
|
|
import {
|
|
AutocompleteClipboardSnippet,
|
|
AutocompleteCodeSnippet,
|
|
AutocompleteDiffSnippet,
|
|
AutocompleteSnippetType,
|
|
} from "./types";
|
|
|
|
export interface SnippetPayload {
|
|
rootPathSnippets: AutocompleteCodeSnippet[];
|
|
importDefinitionSnippets: AutocompleteCodeSnippet[];
|
|
ideSnippets: AutocompleteCodeSnippet[];
|
|
recentlyEditedRangeSnippets: AutocompleteCodeSnippet[];
|
|
diffSnippets: AutocompleteDiffSnippet[];
|
|
clipboardSnippets: AutocompleteClipboardSnippet[];
|
|
}
|
|
|
|
function racePromise<T>(promise: Promise<T[]>): Promise<T[]> {
|
|
const timeoutPromise = new Promise<T[]>((resolve) => {
|
|
setTimeout(() => resolve([]), 100);
|
|
});
|
|
|
|
return Promise.race([promise, timeoutPromise]);
|
|
}
|
|
|
|
// Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems,
|
|
// or they might separately track recently edited ranges)
|
|
async function getIdeSnippets(
|
|
helper: HelperVars,
|
|
ide: IDE,
|
|
getDefinitionsFromLsp: GetLspDefinitionsFunction,
|
|
): Promise<AutocompleteCodeSnippet[]> {
|
|
const ideSnippets = await getDefinitionsFromLsp(
|
|
helper.input.filepath,
|
|
helper.fullPrefix + helper.fullSuffix,
|
|
helper.fullPrefix.length,
|
|
ide,
|
|
helper.lang,
|
|
);
|
|
|
|
if (helper.options.onlyMyCode) {
|
|
const workspaceDirs = await ide.getWorkspaceDirs();
|
|
|
|
return ideSnippets.filter((snippet) =>
|
|
workspaceDirs.some(
|
|
(dir) => !!findUriInDirs(snippet.filepath, [dir]).foundInDir,
|
|
),
|
|
);
|
|
}
|
|
|
|
return ideSnippets;
|
|
}
|
|
|
|
function getSnippetsFromRecentlyEditedRanges(
|
|
helper: HelperVars,
|
|
): AutocompleteCodeSnippet[] {
|
|
if (helper.options.useRecentlyEdited === false) {
|
|
return [];
|
|
}
|
|
|
|
return helper.input.recentlyEditedRanges.map((range) => {
|
|
return {
|
|
filepath: range.filepath,
|
|
content: range.lines.join("\n"),
|
|
type: AutocompleteSnippetType.Code,
|
|
};
|
|
});
|
|
}
|
|
|
|
const getClipboardSnippets = async (
|
|
ide: IDE,
|
|
): Promise<AutocompleteClipboardSnippet[]> => {
|
|
const content = await ide.getClipboardContent();
|
|
|
|
return [content].map((item) => {
|
|
return {
|
|
content: item.text,
|
|
copiedAt: item.copiedAt,
|
|
type: AutocompleteSnippetType.Clipboard,
|
|
};
|
|
});
|
|
};
|
|
|
|
const getDiffSnippets = async (
|
|
ide: IDE,
|
|
): Promise<AutocompleteDiffSnippet[]> => {
|
|
const diff = await ide.getDiff(true);
|
|
|
|
return diff.map((item) => {
|
|
return {
|
|
content: item,
|
|
type: AutocompleteSnippetType.Diff,
|
|
};
|
|
});
|
|
};
|
|
|
|
export const getAllSnippets = async ({
|
|
helper,
|
|
ide,
|
|
getDefinitionsFromLsp,
|
|
contextRetrievalService,
|
|
}: {
|
|
helper: HelperVars;
|
|
ide: IDE;
|
|
getDefinitionsFromLsp: GetLspDefinitionsFunction;
|
|
contextRetrievalService: ContextRetrievalService;
|
|
}): Promise<SnippetPayload> => {
|
|
const recentlyEditedRangeSnippets =
|
|
getSnippetsFromRecentlyEditedRanges(helper);
|
|
|
|
const [
|
|
rootPathSnippets,
|
|
importDefinitionSnippets,
|
|
ideSnippets,
|
|
diffSnippets,
|
|
clipboardSnippets,
|
|
] = await Promise.all([
|
|
racePromise(contextRetrievalService.getRootPathSnippets(helper)),
|
|
racePromise(
|
|
contextRetrievalService.getSnippetsFromImportDefinitions(helper),
|
|
),
|
|
racePromise(getIdeSnippets(helper, ide, getDefinitionsFromLsp)),
|
|
racePromise(getDiffSnippets(ide)),
|
|
racePromise(getClipboardSnippets(ide)),
|
|
]);
|
|
|
|
return {
|
|
rootPathSnippets,
|
|
importDefinitionSnippets,
|
|
ideSnippets,
|
|
recentlyEditedRangeSnippets,
|
|
diffSnippets,
|
|
clipboardSnippets,
|
|
};
|
|
};
|