diff --git a/CHANGELOG.md b/CHANGELOG.md index eb0f059..ae8c1af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Change Log All notable changes to the "crates" extension will be documented in this file. +### 0.0.9 +* Version list (hover) is clickable now. Easier to navigate between versions. + ### 0.0.8 * Better activation event. Listens for "Cargo.toml" as a rool level file. * Refreshes after each save. diff --git a/feature.gif b/feature.gif index e597c46..8c5ed4a 100644 Binary files a/feature.gif and b/feature.gif differ diff --git a/package.json b/package.json index a091484..e5f981e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "crates", "displayName": "crates", "description": "Aims to help developers to manage dependencies while using Cargo.toml. It is only helpful if you are using dependencies from crates.io.", - "version": "0.0.8", + "version": "0.0.9", "publisher": "serayuzgur", "engines": { "vscode": "^1.23.0" @@ -32,6 +32,13 @@ "onLanguage:toml", "workspaceContains:Cargo.toml" ], + "contributes": { + "commands": [{ + "command": "crates.replaceVersion", + "title": "crates: Replace Version" + }] +}, + "main": "./out/extension", "scripts": { "build":"npm run compile", diff --git a/src/extension.ts b/src/extension.ts index 9289804..575f09d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,8 +2,13 @@ /** * This extension helps to manage crate dependency versions. */ -import { window, workspace, ExtensionContext } from "vscode"; +import { + window, + workspace, + ExtensionContext, +} from "vscode"; import tomlListener from "./toml/listener"; +import TomlCommands from "./toml/commands"; export function activate(context: ExtensionContext) { // Add active text editor listener and run once on start. @@ -11,8 +16,11 @@ export function activate(context: ExtensionContext) { workspace.onDidSaveTextDocument(() => { tomlListener(window.activeTextEditor); }); - tomlListener(window.activeTextEditor); + + // Add commands + context.subscriptions.push(TomlCommands.replaceVersion); + } export function deactivate() {} diff --git a/src/toml/commands.ts b/src/toml/commands.ts new file mode 100644 index 0000000..777e168 --- /dev/null +++ b/src/toml/commands.ts @@ -0,0 +1,25 @@ +/** + * Commands related to TOML files. + */ +import { commands, TextEditor, TextEditorEdit, Range } from "vscode"; + +export const replaceVersion = commands.registerTextEditorCommand( + "crates.replaceVersion", + (editor: TextEditor, edit: TextEditorEdit, info) => { + if (editor && info) { + const { fileName } = editor.document; + if (fileName.toLocaleLowerCase().endsWith("cargo.toml")) { + console.log("Replacing", info.item); + edit.replace( + new Range( + editor.document.positionAt(info.start), + editor.document.positionAt(info.end), + ), + info.item, + ); + } + } + }, +); + +export default { replaceVersion }; diff --git a/src/toml/decorations.ts b/src/toml/decorations.ts index 835557b..65b45b9 100644 --- a/src/toml/decorations.ts +++ b/src/toml/decorations.ts @@ -1,7 +1,7 @@ /** * Helps to manage decorations for the TOML files. */ -import { DecorationOptions, Range, TextEditor } from "vscode"; +import { DecorationOptions, Range, TextEditor, MarkdownString } from "vscode"; import { versions } from "../api"; import { statusBarItem } from "../ui/indicators"; @@ -27,12 +27,25 @@ function decoration( const end = regex.lastIndex; const start = regex.lastIndex - match.length; const hasLatest = versions[0] === version; + const versionLinks = versions.map( + item => + `[${item}](command:crates.replaceVersion?${JSON.stringify({ + item: `${crate} = "${item}"`, + start, + end, + })})`, + ); + const hoverMessage = new MarkdownString( + `**Available Versions** \t \n * ${versionLinks.join("\n * ")}`, + ); + hoverMessage.isTrusted = true; + return { range: new Range( editor.document.positionAt(start), editor.document.positionAt(end), ), - hoverMessage: `**Available Versions** \t \n * ${versions.join("\n * ")}`, + hoverMessage, renderOptions: { after: { contentText: hasLatest ? "👍" : `Latest: ${versions[0]}`, diff --git a/src/ui/decorations/latestVersion.ts b/src/ui/decorations.ts similarity index 64% rename from src/ui/decorations/latestVersion.ts rename to src/ui/decorations.ts index 979dfff..0ad8ef9 100644 --- a/src/ui/decorations/latestVersion.ts +++ b/src/ui/decorations.ts @@ -1,13 +1,18 @@ /** - * Decoration to show latest vesion at the right side of the - * depencency. + * Holds common decorations. */ import { window } from "vscode"; -export default (text: string) => +/** + * Decoration to show latest vesion at the right side of the + * depencency. + */ +export const latestVersion = (text: string) => window.createTextEditorDecorationType({ after: { contentText: text, margin: "2em", - } + }, }); + +export default { latestVersion }; diff --git a/src/ui/decorations/index.ts b/src/ui/decorations/index.ts deleted file mode 100644 index 17076b3..0000000 --- a/src/ui/decorations/index.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Holds common decorations. - */ -import latestVersion from "./latestVersion"; - -export default { - latestVersion -}; \ No newline at end of file