-
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.
Group tools by file. Open tool definition on click.
- Loading branch information
Showing
6 changed files
with
128 additions
and
14 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
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 |
---|---|---|
@@ -1,38 +1,141 @@ | ||
import * as path from "node:path"; | ||
import * as vscode from "vscode"; | ||
import type { MiseService } from "../miseService"; | ||
|
||
export class MiseToolsProvider implements vscode.TreeDataProvider<ToolItem> { | ||
private _onDidChangeTreeData: vscode.EventEmitter< | ||
ToolItem | undefined | null | void | ||
> = new vscode.EventEmitter<ToolItem | undefined | null | void>(); | ||
readonly onDidChangeTreeData: vscode.Event< | ||
ToolItem | undefined | null | void | ||
> = this._onDidChangeTreeData.event; | ||
type TreeItem = SourceItem | ToolItem; | ||
|
||
export class MiseToolsProvider implements vscode.TreeDataProvider<TreeItem> { | ||
private _onDidChangeTreeData = new vscode.EventEmitter< | ||
TreeItem | undefined | null | void | ||
>(); | ||
readonly onDidChangeTreeData = this._onDidChangeTreeData.event; | ||
|
||
constructor(private miseService: MiseService) {} | ||
|
||
refresh(): void { | ||
this._onDidChangeTreeData.fire(); | ||
} | ||
|
||
getTreeItem(element: ToolItem): vscode.TreeItem { | ||
getTreeItem(element: TreeItem): vscode.TreeItem { | ||
return element; | ||
} | ||
|
||
async getChildren(): Promise<ToolItem[]> { | ||
const tools = await this.miseService.getTools(); | ||
return tools.map((tool) => new ToolItem(tool)); | ||
async getChildren(element?: TreeItem): Promise<TreeItem[]> { | ||
if (!element) { | ||
const tools = await this.miseService.getTools(); | ||
const toolsBySource = this.groupToolsBySource(tools); | ||
|
||
return Object.entries(toolsBySource).map( | ||
([source, tools]) => new SourceItem(source, tools), | ||
); | ||
} | ||
|
||
if (element instanceof SourceItem) { | ||
return element.tools.map((tool) => new ToolItem(tool)); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private groupToolsBySource(tools: MiseTool[]): Record<string, MiseTool[]> { | ||
return tools.reduce((acc: Record<string, MiseTool[]>, tool: MiseTool) => { | ||
const source = tool.source?.path || "Unknown"; | ||
if (!acc[source]) { | ||
acc[source] = []; | ||
} | ||
acc[source].push(tool); | ||
return acc; | ||
}, {}); | ||
} | ||
} | ||
|
||
class SourceItem extends vscode.TreeItem { | ||
constructor( | ||
public readonly source: string, | ||
public readonly tools: MiseTool[], | ||
) { | ||
super(path.basename(source), vscode.TreeItemCollapsibleState.Expanded); | ||
|
||
this.tooltip = `Source: ${source} | ||
Number of tools: ${tools.length}`; | ||
|
||
this.iconPath = new vscode.ThemeIcon("folder"); | ||
this.contextValue = "source"; | ||
this.description = `(${tools.length} tools)`; | ||
} | ||
} | ||
|
||
class ToolItem extends vscode.TreeItem { | ||
constructor(tool: MiseTool) { | ||
super(`${tool.name} ${tool.version}`, vscode.TreeItemCollapsibleState.None); | ||
|
||
this.tooltip = `Tool: ${tool.name} | ||
Version: ${tool.version} | ||
Requested Version: ${tool.requested_version} | ||
Source: ${tool.source?.path || "Unknown"} | ||
Activated: ${tool.active} | ||
Installed: ${tool.installed} | ||
Install Path: ${tool.install_path}`; | ||
|
||
this.iconPath = this.getToolIcon(tool); | ||
|
||
if (tool.source?.path) { | ||
this.command = { | ||
command: "mise.openToolDefinition", | ||
title: "Open Tool Definition", | ||
arguments: [tool], | ||
}; | ||
} | ||
} | ||
|
||
private getToolIcon(tool: MiseTool): vscode.ThemeIcon { | ||
if (!tool.installed) { | ||
return new vscode.ThemeIcon("circle-outline"); | ||
} | ||
if (tool.active) { | ||
return new vscode.ThemeIcon("check"); | ||
} | ||
return new vscode.ThemeIcon("circle-filled"); | ||
} | ||
} | ||
|
||
export function registerCommands(context: vscode.ExtensionContext) { | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand( | ||
"mise.openToolDefinition", | ||
async (tool: MiseTool) => { | ||
if (!tool.source?.path) { | ||
return; | ||
} | ||
|
||
const document = await vscode.workspace.openTextDocument( | ||
tool.source.path, | ||
); | ||
const editor = await vscode.window.showTextDocument(document); | ||
|
||
let line = 0; | ||
for (let i = 0; i < editor.document.getText().split("\n").length; i++) { | ||
const l = editor.document.getText().split("\n")[i]; | ||
const [firstWord] = l.replace(/\s/g, "").replace(/"/g, "").split("="); | ||
if (firstWord === tool.name) { | ||
line = i + 1; | ||
break; | ||
} | ||
} | ||
|
||
if (line) { | ||
const position = new vscode.Position(Math.max(0, line - 1), 0); | ||
const position2 = new vscode.Position( | ||
Math.max(0, line - 1), | ||
tool.name.includes(":") ? tool.name.length + 2 : tool.name.length, | ||
); | ||
editor.selection = new vscode.Selection(position, position2); | ||
editor.revealRange( | ||
new vscode.Range(position, position2), | ||
vscode.TextEditorRevealType.InCenter, | ||
); | ||
} | ||
}, | ||
), | ||
); | ||
} |
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