| 1 | import { IDE } from "core"; |
| 2 | import { getUriPathBasename, getLastNPathParts } from "core/util/uri"; |
| 3 | import vscode from "vscode"; |
| 4 | |
| 5 | import { FileSearch } from "../util/FileSearch"; |
| 6 | |
| 7 | class PromptFilesCompletionItemProvider |
| 8 | implements vscode.CompletionItemProvider |
| 9 | { |
| 10 | constructor( |
| 11 | private fileSearch: FileSearch, |
| 12 | private ide: IDE, |
| 13 | ) {} |
| 14 | async provideCompletionItems( |
| 15 | document: vscode.TextDocument, |
| 16 | position: vscode.Position, |
| 17 | token: vscode.CancellationToken, |
| 18 | context: vscode.CompletionContext, |
| 19 | ) { |
| 20 | const linePrefix = |
| 21 | document |
| 22 | .lineAt(position) |
| 23 | .text.substr(0, position.character) |
| 24 | .split(" ") |
| 25 | .pop() || ""; |
| 26 | |
| 27 | // Check if the last character typed is '@' |
| 28 | if (!linePrefix.includes("@")) { |
| 29 | return undefined; |
| 30 | } |
| 31 | |
| 32 | const searchText = linePrefix.split("@").pop() || ""; |
| 33 | const files = this.fileSearch.search(searchText); |
| 34 | |
| 35 | if (files.length === 0) { |
| 36 | const openFiles = await this.ide.getOpenFiles(); |
| 37 | files.push( |
| 38 | ...openFiles.map((fileUri) => ({ |
| 39 | id: fileUri, |
| 40 | relativePath: vscode.workspace.asRelativePath(fileUri), |
| 41 | })), |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | // Provide completion items |
| 46 | return [ |
| 47 | { |
| 48 | label: "terminal", |
| 49 | detail: "Contents of terminal", |
| 50 | sortText: "...", |
| 51 | kind: vscode.CompletionItemKind.Field, |
| 52 | }, |
| 53 | { |
| 54 | label: "tree", |
| 55 | detail: "File tree of workspace", |
| 56 | sortText: "...", |
| 57 | kind: vscode.CompletionItemKind.Field, |
| 58 | }, |
| 59 | { |
| 60 | label: "open", |
| 61 | detail: "All open files", |
| 62 | sortText: "...", |
| 63 | kind: vscode.CompletionItemKind.Field, |
| 64 | }, |
| 65 | { |
| 66 | label: "os", |
| 67 | detail: "Operating system information", |
| 68 | sortText: "...", |
| 69 | kind: vscode.CompletionItemKind.Field, |
| 70 | }, |
| 71 | { |
| 72 | label: "problems", |
| 73 | detail: "Problems in the current file", |
| 74 | sortText: "...", |
| 75 | kind: vscode.CompletionItemKind.Field, |
| 76 | }, |
| 77 | { |
| 78 | label: "currentFile", |
| 79 | detail: "Current file", |
| 80 | sortText: "...", |
| 81 | kind: vscode.CompletionItemKind.Field, |
| 82 | }, |
| 83 | { |
| 84 | label: "repo-map", |
| 85 | detail: "Map of files in your repo", |
| 86 | sortText: "...", |
| 87 | kind: vscode.CompletionItemKind.Field, |
| 88 | }, |
| 89 | ...files.map((file) => { |
| 90 | return { |
| 91 | label: getUriPathBasename(file.id), |
| 92 | detail: getLastNPathParts(file.relativePath, 2), |
| 93 | insertText: file.relativePath, |
| 94 | kind: vscode.CompletionItemKind.File, |
| 95 | }; |
| 96 | }), |
| 97 | ]; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | function registerPromptFilesCompletionProvider( |
| 102 | context: vscode.ExtensionContext, |
| 103 | fileSearch: FileSearch, |
| 104 | ide: IDE, |
| 105 | ) { |
| 106 | let provider = vscode.languages.registerCompletionItemProvider( |
| 107 | "promptLanguage", |
| 108 | new PromptFilesCompletionItemProvider(fileSearch, ide), |
| 109 | "@", // Trigger completion when '@' is typed |
| 110 | ); |
| 111 | |
| 112 | context.subscriptions.push(provider); |
| 113 | } |
| 114 | |
| 115 | class YamlKeysCompletionItemProvider implements vscode.CompletionItemProvider { |
| 116 | private yamlKeys: { key: string; description: string }[] = [ |
| 117 | { |
| 118 | key: "name", |
| 119 | description: "The name of the prompt template", |
| 120 | }, |
| 121 | { |
| 122 | key: "description", |
| 123 | description: "A description of what this prompt template does", |
| 124 | }, |
| 125 | { |
| 126 | key: "version", |
| 127 | description: ".prompt file version. Either 1 (legacy) or 2", |
| 128 | }, |
| 129 | ]; |
| 130 | |
| 131 | async provideCompletionItems( |
| 132 | document: vscode.TextDocument, |
| 133 | position: vscode.Position, |
| 134 | token: vscode.CancellationToken, |
| 135 | context: vscode.CompletionContext, |
| 136 | ) { |
| 137 | const lineText = document.lineAt(position).text; |
| 138 | const textBeforeCursor = lineText.substring(0, position.character); |
| 139 | |
| 140 | // 0. If no delimiter in the file, return no completions |
| 141 | const fullContent = document.getText(); |
| 142 | if (!fullContent.includes("---")) { |
| 143 | return undefined; |
| 144 | } |
| 145 | |
| 146 | // 1. Check if the cursor is in YAML section (before --- delimiter) |
| 147 | const beforeDelimiter = isCursorBeforeDelimiter(document, position); |
| 148 | |
| 149 | if (!beforeDelimiter) { |
| 150 | return undefined; |
| 151 | } |
| 152 | |
| 153 | // 2. Check if there is no colon before the cursor on this line |
| 154 | if (textBeforeCursor.includes(":")) { |
| 155 | return undefined; |
| 156 | } |
| 157 | |
| 158 | // 3. Provide completion items for YAML keys |
| 159 | const completionItems = this.yamlKeys.map((key) => { |
| 160 | const item = new vscode.CompletionItem( |
| 161 | key.key, |
| 162 | vscode.CompletionItemKind.Property, |
| 163 | ); |
| 164 | item.documentation = new vscode.MarkdownString(key.description); |
| 165 | item.insertText = key.key + ": "; |
| 166 | return item; |
| 167 | }); |
| 168 | |
| 169 | return completionItems; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Helper function to determine if the cursor is before the '---' delimiter |
| 174 | function isCursorBeforeDelimiter( |
| 175 | document: vscode.TextDocument, |
| 176 | position: vscode.Position, |
| 177 | ): boolean { |
| 178 | const delimiter = "---"; |
| 179 | for (let i = 0; i < document.lineCount; i++) { |
| 180 | const lineText = document.lineAt(i).text.trim(); |
| 181 | if (lineText === delimiter) { |
| 182 | // Cursor is before the delimiter |
| 183 | return position.line < i; |
| 184 | } |
| 185 | } |
| 186 | // Delimiter not found; assume entire document is YAML |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | function registerYamlKeysCompletionProvider(context: vscode.ExtensionContext) { |
| 191 | const provider = vscode.languages.registerCompletionItemProvider( |
| 192 | "promptLanguage", |
| 193 | new YamlKeysCompletionItemProvider(), |
| 194 | ...Array.from("abcdefghijklmnopqrstuvwxyz"), // Trigger on all lowercase letters |
| 195 | ); |
| 196 | |
| 197 | context.subscriptions.push(provider); |
| 198 | } |
| 199 | |
| 200 | // Helper function to determine if the position is within the YAML header |
| 201 | function isPositionInYamlHeader( |
| 202 | document: vscode.TextDocument, |
| 203 | position: vscode.Position, |
| 204 | ): boolean { |
| 205 | const delimiter = "---"; |
| 206 | for (let i = 0; i < document.lineCount; i++) { |
| 207 | const lineText = document.lineAt(i).text.trim(); |
| 208 | if (lineText === delimiter) { |
| 209 | // Cursor is before the delimiter |
| 210 | return position.line < i; |
| 211 | } |
| 212 | } |
| 213 | // Delimiter not found; assume entire document is YAML |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | export function registerAllPromptFilesCompletionProviders( |
| 218 | context: vscode.ExtensionContext, |
| 219 | fileSearch: FileSearch, |
| 220 | ide: IDE, |
| 221 | ) { |
| 222 | registerPromptFilesCompletionProvider(context, fileSearch, ide); |
| 223 | registerYamlKeysCompletionProvider(context); |
| 224 | |
| 225 | context.subscriptions.push( |
| 226 | vscode.workspace.onDidChangeTextDocument((event) => { |
| 227 | const editor = vscode.window.activeTextEditor; |
| 228 | if (!editor || event.document !== editor.document) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | const languageId = editor.document.languageId; |
| 233 | if (languageId !== "promptLanguage") { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | for (const change of event.contentChanges) { |
| 238 | // Get position after the change by applying the change length |
| 239 | const isDeletion = change.text.length <= 0; |
| 240 | const lines = change.text.split("\n"); |
| 241 | const positionAfterChange = isDeletion |
| 242 | ? change.range.start |
| 243 | : new vscode.Position( |
| 244 | change.range.start.line + lines.length - 1, |
| 245 | lines.length === 1 |
| 246 | ? change.range.start.character + change.text.length |
| 247 | : lines[lines.length - 1].length, |
| 248 | ); |
| 249 | |
| 250 | // Use the updated document state |
| 251 | const line = event.document.lineAt(positionAfterChange.line); |
| 252 | |
| 253 | // Check if we're in the YAML header using current document state |
| 254 | const inYamlHeader = isPositionInYamlHeader( |
| 255 | event.document, |
| 256 | positionAfterChange, |
| 257 | ); |
| 258 | // Check if we're at the first character of the line |
| 259 | const atLineStart = positionAfterChange.character === 0; |
| 260 | // Check if the current line is empty (excluding whitespace) |
| 261 | const lineIsEmpty = line.text.trim() === ""; |
| 262 | |
| 263 | if (inYamlHeader && atLineStart && lineIsEmpty) { |
| 264 | // Delay slightly to ensure the editor updates before triggering suggestions |
| 265 | setTimeout(() => { |
| 266 | // Trigger suggestions |
| 267 | vscode.commands.executeCommand("editor.action.triggerSuggest"); |
| 268 | }, 50); |
| 269 | } |
| 270 | } |
| 271 | }), |
| 272 | ); |
| 273 | } |