Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
refactor, clickable hover list, replace command. and new gif
Browse files Browse the repository at this point in the history
  • Loading branch information
serayuzgur committed May 8, 2018
1 parent 5bdcc5c commit 123d924
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Binary file modified feature.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 10 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
/**
* 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.
context.subscriptions.push(window.onDidChangeActiveTextEditor(tomlListener));
workspace.onDidSaveTextDocument(() => {
tomlListener(window.activeTextEditor);
});

tomlListener(window.activeTextEditor);

// Add commands
context.subscriptions.push(TomlCommands.replaceVersion);

}

export function deactivate() {}
25 changes: 25 additions & 0 deletions src/toml/commands.ts
Original file line number Diff line number Diff line change
@@ -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 };
17 changes: 15 additions & 2 deletions src/toml/decorations.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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]}`,
Expand Down
13 changes: 9 additions & 4 deletions src/ui/decorations/latestVersion.ts → src/ui/decorations.ts
Original file line number Diff line number Diff line change
@@ -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 };
8 changes: 0 additions & 8 deletions src/ui/decorations/index.ts

This file was deleted.

0 comments on commit 123d924

Please sign in to comment.