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

Commit

Permalink
[#1] fixed.
Browse files Browse the repository at this point in the history
redecorates on document change
fetches target  dependencies
  • Loading branch information
serayuzgur committed May 16, 2018
1 parent 123d924 commit 4b76a5d
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ src/**
.gitignore
tsconfig.json
vsc-extension-quickstart.md
tslint.json
tslint.json
sample/**
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log
All notable changes to the "crates" extension will be documented in this file.

### 0.0.10
* Complex Cargo.toml error fixed. [#1](https://github.com/serayuzgur/crates/issues/1)
* Support for target dependencies. (not all usage cases)
* Re-decorates on edit. (It was on save before)

### 0.0.9
* Version list (hover) is clickable now. Easier to navigate between versions.

Expand Down
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

## Features

Displays the latest version of the crate next to it and shows all versions on hover.
Displays the latest version of the crate next to it and shows all versions (clickable) on hover.
Aims to be fast, simple.

![ss](https://github.com/serayuzgur/crates/raw/master/feature.gif)

Expand All @@ -17,4 +18,37 @@ No settings for now.

## Known Issues

*
* TOML must be valid.

* For target dependencies

**Supported:**

```toml
[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
cocoa = "0.15.0"
core-foundation = "0.6.0"
core-graphics = "0.14.0"
```

**Not Supported:**

```toml
[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"
features = [
"winnt",
"winuser",
"wingdi",
"shellapi",
"dwmapi",
"processthreadsapi",
"libloaderapi",
"windowsx",
"hidusage",
"combaseapi",
"objbase",
"unknwnbase",
]
```
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
"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.9",
"version": "0.0.10",
"publisher": "serayuzgur",
"engines": {
"vscode": "^1.23.0"
},
"repository": {
"type": "git",
"url": "https://github.com/serayuzgur/crates.git"
"type": "git",
"url": "https://github.com/serayuzgur/crates.git"
},
"icon": "icon.png",

"keywords": [
"cargo",
"crates",
Expand All @@ -33,15 +32,16 @@
"workspaceContains:Cargo.toml"
],
"contributes": {
"commands": [{
"commands": [
{
"command": "crates.replaceVersion",
"title": "crates: Replace Version"
}]
},

}
]
},
"main": "./out/extension",
"scripts": {
"build":"npm run compile",
"build": "npm run compile",
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
Expand All @@ -52,12 +52,12 @@
"typescript": "2.8.3",
"vscode": "1.1.17",
"tslint": "5.10.0",
"@types/node": "10.0.4",
"@types/node": "10.1.0",
"@types/mocha": "5.2.0"
},
"dependencies": {
"@types/request-promise": "4.1.41",
"request": "^2.85.0",
"request": "2.86.0",
"request-promise": "4.2.2",
"toml": "2.3.3"
}
Expand Down
59 changes: 59 additions & 0 deletions sample/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[package]
name = "winit"
version = "0.14.0"
authors = ["The winit contributors, Pierre Krieger <[email protected]>"]
description = "Cross-platform window creation library."
keywords = ["windowing"]
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/tomaka/winit"
documentation = "https://docs.rs/winit"
categories = ["gui"]

[package.metadata.docs.rs]
features = ["icon_loading"]

[features]
icon_loading = ["image"]

[dependencies]
lazy_static = "1.0.0"
libc = "0.2.37"
image = {version = "0.10.0",optional = true}

[target.'cfg(target_os = "android")'.dependencies.android_glue]
version = "0.2"

[target.'cfg(target_os = "ios")'.dependencies]
objc = "0.2.2"

[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
cocoa = "0.15.0"
core-foundation = "0.6.0"
core-graphics = "0.8.1"


[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"
features = [
"winnt",
"winuser",
"wingdi",
"shellapi",
"dwmapi",
"processthreadsapi",
"libloaderapi",
"windowsx",
"hidusage",
"combaseapi",
"objbase",
"unknwnbase",
]

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))'.dependencies]
wayland-client = {version = "0.10.3",features = ["dlopen","egl","cursor"]}
smithay-client-toolkit = "0.2.1"
x11-dl = "2.17.5"
parking_lot = "0.5.5"
percent-encoding = "1.0.1"
14 changes: 10 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ import {
window,
workspace,
ExtensionContext,
TextDocumentChangeEvent,
} 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);
});

context.subscriptions.push(
workspace.onDidChangeTextDocument((e: TextDocumentChangeEvent) => {
const { fileName } = e.document;
if (fileName.toLocaleLowerCase().endsWith("cargo.toml")) {
tomlListener(window.activeTextEditor);
}
}),
);
tomlListener(window.activeTextEditor);

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

}

export function deactivate() {}
5 changes: 4 additions & 1 deletion src/toml/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
*/
import { commands, TextEditor, TextEditorEdit, Range } from "vscode";

export const status = { inProgress: false };

export const replaceVersion = commands.registerTextEditorCommand(
"crates.replaceVersion",
(editor: TextEditor, edit: TextEditorEdit, info) => {
if (editor && info) {
if (editor && info && !status.inProgress) {
const { fileName } = editor.document;
if (fileName.toLocaleLowerCase().endsWith("cargo.toml")) {
status.inProgress = true;
console.log("Replacing", info.item);
edit.replace(
new Range(
Expand Down
32 changes: 21 additions & 11 deletions src/toml/decorations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,36 @@ import { statusBarItem } from "../ui/indicators";
function decoration(
editor: TextEditor,
crate: string,
version: string,
version: string | any,
versions: string[],
) {
const regex = new RegExp(`${crate}.*=.*"${version}"`, "g");
// Also handle json valued dependencies
const regex = new RegExp(`${crate}.*=.*`, "g");
const matches = regex.exec(editor.document.getText());
if (!matches || matches.length === 0 || !versions) {
return;
}
const match = matches[0];
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 isVersionString = typeof version === "string";
const hasLatest =
versions[0] === (isVersionString ? version : version.version);
const versionLinks = versions.map(item => {
let template;
if (isVersionString) {
template = `"${item}"`;
} else {
template = { ...version };
template["version"] = item;
template = JSON.stringify({ ...template }).replace(/\"([^(\")"]+)\":/g, "$1 = ");
}
return `[${item}](command:crates.replaceVersion?${JSON.stringify({
item: `${crate} = ${template}`,
start,
end,
})})`;
});
const hoverMessage = new MarkdownString(
`**Available Versions** \t \n * ${versionLinks.join("\n * ")}`,
);
Expand Down
19 changes: 17 additions & 2 deletions src/toml/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { parse } from "toml";
import { statusBarItem } from "../ui/indicators";
import decorators from "../ui/decorations";
import { dependencies } from "./decorations";
import { status } from "./commands";

let decoration: TextEditorDecorationType;

Expand All @@ -16,9 +17,21 @@ function parseAndDecorate(editor: TextEditor) {
console.log("Parsing... ", fileName);
statusBarItem.setText("Fetching crates.io");
const text = editor.document.getText();
console.log("Parsing OK : ", fileName);
const toml = parse(text);
const tomlDependencies = toml["dependencies"];

// parse target dependencies and add to dependencies
const targets = toml["target"];
Object.keys(targets).map(key => {
const target = targets[key];
if (target.dependencies) {
Object.keys(target.dependencies).map(
(key2: any) => (tomlDependencies[key2] = target.dependencies[key2]),
);
}
});
try {
dependencies(editor, parse(text)["dependencies"], options => {
dependencies(editor, tomlDependencies, options => {
if (decoration) {
decoration.dispose();
}
Expand All @@ -34,11 +47,13 @@ export default function(editor: TextEditor | undefined): void {
if (editor) {
const { fileName } = editor.document;
if (fileName.toLocaleLowerCase().endsWith("cargo.toml")) {
status.inProgress = true;
statusBarItem.show();
parseAndDecorate(editor);
} else {
statusBarItem.hide();
}
status.inProgress = false;
} else {
console.log("No active editor found.");
}
Expand Down

0 comments on commit 4b76a5d

Please sign in to comment.