-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add autocompletion for tool names and versions
- Loading branch information
Showing
7 changed files
with
174 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as vscode from "vscode"; | ||
import type { MiseService } from "../miseService"; | ||
import { getCleanedToolName } from "../utils/miseUtilts"; | ||
|
||
export class ToolCompletionProvider implements vscode.CompletionItemProvider { | ||
private miseService: MiseService; | ||
|
||
constructor(miseService: MiseService) { | ||
this.miseService = miseService; | ||
} | ||
|
||
async provideCompletionItems( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
) { | ||
const [tools, backends] = await Promise.all([ | ||
this.miseService.miseRegistry(), | ||
this.miseService.miseBackends(), | ||
]); | ||
|
||
const linePrefix = document | ||
.lineAt(position) | ||
.text.substring(0, position.character); | ||
|
||
let inToolsSection = false; | ||
for (let i = position.line; i >= 0; i--) { | ||
const line = document.lineAt(i).text.trim(); | ||
if (line === "[tools]") { | ||
inToolsSection = true; | ||
break; | ||
} | ||
if (line.startsWith("[") && line !== "[tools]") { | ||
break; | ||
} | ||
} | ||
|
||
if (!inToolsSection) { | ||
return []; | ||
} | ||
|
||
if (!linePrefix.includes("=")) { | ||
const toolsCompletions = tools | ||
.filter((tool) => tool.short !== undefined) | ||
.map((tool) => { | ||
const completionItem = new vscode.CompletionItem( | ||
{ | ||
label: tool.short as string, | ||
description: tool.full, | ||
}, | ||
vscode.CompletionItemKind.Module, | ||
); | ||
completionItem.insertText = `${tool.short} = `; | ||
completionItem.command = { | ||
command: "editor.action.triggerSuggest", | ||
title: "Re-trigger completions", | ||
}; | ||
return completionItem; | ||
}); | ||
|
||
const backendsCompletions = backends.map((backend) => { | ||
const completionItem = new vscode.CompletionItem( | ||
{ label: `'${backend}:`, description: `${backend} backend` }, | ||
vscode.CompletionItemKind.Value, | ||
); | ||
completionItem.insertText = `'${backend}:`; | ||
return completionItem; | ||
}); | ||
|
||
return toolsCompletions.concat(backendsCompletions); | ||
} | ||
|
||
const toolMatch = linePrefix.match( | ||
/([a-z/'"-0-9:]*)\s*=\s*(["']?)([^"']*)$/, | ||
); | ||
if (!toolMatch) { | ||
return []; | ||
} | ||
|
||
const [, toolName, existingQuote, partial] = toolMatch; | ||
if (!toolName) { | ||
return []; | ||
} | ||
|
||
const cleanedToolName = getCleanedToolName(toolName); | ||
const versions = await this.miseService.listRemoteVersions(cleanedToolName); | ||
|
||
return ["latest", ...versions] | ||
.filter((version) => { | ||
if (partial) { | ||
return version.startsWith(partial.replace(/['"]/, "")); | ||
} | ||
return true; | ||
}) | ||
.map((version, i) => { | ||
const completionItem = new vscode.CompletionItem( | ||
version, | ||
vscode.CompletionItemKind.Value, | ||
); | ||
completionItem.sortText = i.toString(); | ||
completionItem.insertText = existingQuote ? version : `'${version}'`; | ||
return completionItem; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters