Elffuss BraidHub

kikocis/continue public

71fd2e7d4456 · 4.8 KiBRawPermalink
1import { IDE } from "../../index";
2import { findUriInDirs } from "../../util/uri";
3import { ContextRetrievalService } from "../context/ContextRetrievalService";
4import { GetLspDefinitionsFunction } from "../types";
5import { HelperVars } from "../util/HelperVars";
6
7import {
8 AutocompleteClipboardSnippet,
9 AutocompleteCodeSnippet,
10 AutocompleteDiffSnippet,
11 AutocompleteSnippetType,
12} from "./types";
13
14const IDE_SNIPPETS_ENABLED = false; // ideSnippets is not used, so it's temporarily disabled
15
16export interface SnippetPayload {
17 rootPathSnippets: AutocompleteCodeSnippet[];
18 importDefinitionSnippets: AutocompleteCodeSnippet[];
19 ideSnippets: AutocompleteCodeSnippet[];
20 recentlyEditedRangeSnippets: AutocompleteCodeSnippet[];
21 recentlyVisitedRangesSnippets: AutocompleteCodeSnippet[];
22 diffSnippets: AutocompleteDiffSnippet[];
23 clipboardSnippets: AutocompleteClipboardSnippet[];
24}
25
26function racePromise<T>(promise: Promise<T[]>): Promise<T[]> {
27 const timeoutPromise = new Promise<T[]>((resolve) => {
28 setTimeout(() => resolve([]), 100);
29 });
30
31 return Promise.race([promise, timeoutPromise]);
32}
33
34class DiffSnippetsCache {
35 private cache: Map<number, any> = new Map();
36 private lastTimestamp: number = 0;
37
38 public set<T>(timestamp: number, value: T): T {
39 // Clear old cache entry if exists
40 if (this.lastTimestamp !== timestamp) {
41 this.cache.clear();
42 }
43 this.lastTimestamp = timestamp;
44 this.cache.set(timestamp, value);
45 return value;
46 }
47
48 public get(timestamp: number): any | undefined {
49 return this.cache.get(timestamp);
50 }
51}
52
53const diffSnippetsCache = new DiffSnippetsCache();
54
55// Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems,
56// or they might separately track recently edited ranges)
57async function getIdeSnippets(
58 helper: HelperVars,
59 ide: IDE,
60 getDefinitionsFromLsp: GetLspDefinitionsFunction,
61): Promise<AutocompleteCodeSnippet[]> {
62 const ideSnippets = await getDefinitionsFromLsp(
63 helper.input.filepath,
64 helper.fullPrefix + helper.fullSuffix,
65 helper.fullPrefix.length,
66 ide,
67 helper.lang,
68 );
69
70 if (helper.options.onlyMyCode) {
71 const workspaceDirs = await ide.getWorkspaceDirs();
72
73 return ideSnippets.filter((snippet) =>
74 workspaceDirs.some(
75 (dir) => !!findUriInDirs(snippet.filepath, [dir]).foundInDir,
76 ),
77 );
78 }
79
80 return ideSnippets;
81}
82
83function getSnippetsFromRecentlyEditedRanges(
84 helper: HelperVars,
85): AutocompleteCodeSnippet[] {
86 if (helper.options.useRecentlyEdited === false) {
87 return [];
88 }
89
90 return helper.input.recentlyEditedRanges.map((range) => {
91 return {
92 filepath: range.filepath,
93 content: range.lines.join("\n"),
94 type: AutocompleteSnippetType.Code,
95 };
96 });
97}
98
99const getClipboardSnippets = async (
100 ide: IDE,
101): Promise<AutocompleteClipboardSnippet[]> => {
102 const content = await ide.getClipboardContent();
103
104 return [content].map((item) => {
105 return {
106 content: item.text,
107 copiedAt: item.copiedAt,
108 type: AutocompleteSnippetType.Clipboard,
109 };
110 });
111};
112
113const getDiffSnippets = async (
114 ide: IDE,
115): Promise<AutocompleteDiffSnippet[]> => {
116 const currentTimestamp = ide.getLastFileSaveTimestamp ?
117 ide.getLastFileSaveTimestamp() :
118 Math.floor(Date.now() / 10000) * 10000; // Defaults to update once in every 10 seconds
119
120 // Check cache first
121 const cached = diffSnippetsCache.get(currentTimestamp) as AutocompleteDiffSnippet[];
122
123 if (cached) {
124 return cached;
125 }
126
127 let diff: string[] = [];
128 try {
129 diff = await ide.getDiff(true);
130 } catch (e) {
131 console.error("Error getting diff for autocomplete", e);
132 }
133
134 return diffSnippetsCache.set(currentTimestamp, diff.map((item) => {
135 return {
136 content: item,
137 type: AutocompleteSnippetType.Diff,
138 };
139 }));
140
141};
142
143export const getAllSnippets = async ({
144 helper,
145 ide,
146 getDefinitionsFromLsp,
147 contextRetrievalService,
148}: {
149 helper: HelperVars;
150 ide: IDE;
151 getDefinitionsFromLsp: GetLspDefinitionsFunction;
152 contextRetrievalService: ContextRetrievalService;
153}): Promise<SnippetPayload> => {
154 const recentlyEditedRangeSnippets =
155 getSnippetsFromRecentlyEditedRanges(helper);
156
157 const [
158 rootPathSnippets,
159 importDefinitionSnippets,
160 ideSnippets,
161 diffSnippets,
162 clipboardSnippets,
163 ] = await Promise.all([
164 racePromise(contextRetrievalService.getRootPathSnippets(helper)),
165 racePromise(contextRetrievalService.getSnippetsFromImportDefinitions(helper)),
166 IDE_SNIPPETS_ENABLED ? racePromise(getIdeSnippets(helper, ide, getDefinitionsFromLsp)) : [],
167 racePromise(getDiffSnippets(ide)),
168 racePromise(getClipboardSnippets(ide)),
169 ]);
170
171 return {
172 rootPathSnippets,
173 importDefinitionSnippets,
174 ideSnippets,
175 recentlyEditedRangeSnippets,
176 diffSnippets,
177 clipboardSnippets,
178 recentlyVisitedRangesSnippets: helper.input.recentlyVisitedRanges,
179 };
180};