you better run

This commit is contained in:
Aleksey Kladov 2018-08-27 22:52:43 +03:00
parent 5751815314
commit 422efe59af
13 changed files with 2654 additions and 2633 deletions

9
code/.gitignore vendored
View file

@ -1,7 +1,4 @@
target out
index.node node_modules
artifacts.json .vscode-test/
*.vsix *.vsix
out/*
node_modules/*
log/*

View file

@ -1,4 +0,0 @@
runtime = electron
target = 1.7.9
target_arch = x64
disturl = https://atom.io/download/atom-shell

View file

@ -1,22 +1,36 @@
// A launch configuration that compiles the extension and then opens it inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{ {
// Use IntelliSense to learn about possible attributes. "version": "0.2.0",
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "Launch Extension", "name": "Extension",
"type": "extensionHost", "type": "extensionHost",
"request": "launch", "request": "launch",
"runtimeExecutable": "${execPath}", "runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath='./'"], "args": [
"env": { "--extensionDevelopmentPath=${workspaceFolder}"
"RUST_LOG": "m=trace" ],
}, "outFiles": [
"stopOnEntry": false, "${workspaceFolder}/out/**/*.js"
"sourceMaps": true, ],
"outFiles": [ "./out/src/**/*.js" ], "preLaunchTask": "npm: watch"
"preLaunchTask": "npm"
}, },
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test"
],
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
],
"preLaunchTask": "npm: watch"
}
] ]
} }

View file

@ -1,10 +1,11 @@
// Place your settings in this file to overwrite default and user settings. // Place your settings in this file to overwrite default and user settings.
{ {
"files.exclude": { "files.exclude": {
"out": true "out": false // set this to true to hide the "out" folder with the compiled JS files
// "node_modules": true
}, },
"search.exclude": { "search.exclude": {
"out": true // set this to false to include "out" folder in search results "out": true // set this to false to include "out" folder in search results
} },
} // Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off"
}

View file

@ -1,31 +1,20 @@
// See https://go.microsoft.com/fwlink/?LinkId=733558
// Available variables which can be used inside of strings. // for the documentation about the tasks.json format
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
// A task runner that calls a custom npm script that compiles the extension.
{ {
"version": "0.2.0", "version": "2.0.0",
"tasks": [
// we want to run npm {
"command": "npm", "type": "npm",
"script": "watch",
// the command is a shell script "problemMatcher": "$tsc-watch",
"isShellCommand": true, "isBackground": true,
"presentation": {
// show the output window only if unrecognized errors occur. "reveal": "never"
"showOutput": "silent", },
"group": {
// we run the custom script "compile" as defined in package.json "kind": "build",
"args": ["run", "compile",], "isDefault": true
}
// The tsc compiler is started in watching mode }
"isBackground": true, ]
}
// use the standard tsc in watch mode problem matcher to find compile problems in the output.
"problemMatcher": "$tsc-watch"
}

9
code/.vscodeignore Normal file
View file

@ -0,0 +1,9 @@
.vscode/**
.vscode-test/**
out/test/**
out/**/*.map
src/**
.gitignore
tsconfig.json
vsc-extension-quickstart.md
tslint.json

4728
code/package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,138 +1,149 @@
{ {
"name": "libsyntax-rust", "name": "rcf-lsp",
"displayName": "libsyntax-rust", "displayName": "rcf-lsp",
"description": "An experimental Rust plugin for VS Code based on libsyntax2", "description": "",
"license": "MIT", "version": "0.0.1",
"repository": "http://github.com/matklad/libsyntax2/", "publisher": "matklad",
"version": "0.0.1", "repository": {
"publisher": "matklad", "url": "https://github.com/matklad/libsyntax2/"
"engines": { },
"vscode": "^1.26.0" "categories": [
}, "Other"
"scripts": { ],
"vscode:prepublish": "tsc -p ./", "engines": {
"compile": "tsc -watch -p ./", "vscode": "^1.26.0"
"postinstall": "node ./node_modules/vscode/bin/install" },
}, "scripts": {
"dependencies": { "vscode:prepublish": "npm run compile",
"vscode-languageclient": "^4.4.0" "compile": "tsc -p ./",
}, "watch": "tsc -watch -p ./",
"devDependencies": { "postinstall": "node ./node_modules/vscode/bin/install"
"@types/node": "^7.0.56", },
"typescript": "^2.9.1", "dependencies": {
"vsce": "^1.42.0", "vscode-languageclient": "^4.4.0"
"vscode": "^1.1.18" },
}, "devDependencies": {
"main": "./out/src/extension", "typescript": "^2.6.1",
"activationEvents": [ "vscode": "^1.1.21",
"onLanguage:rust" "vsce": "^1.42.0",
], "@types/node": "^8.10.25",
"contributes": { "@types/mocha": "^2.2.42"
"taskDefinitions": [ },
{ "activationEvents": [
"type": "cargo", "onLanguage:rust"
"required": [ ],
"command" "main": "./out/extension",
"contributes": {
"taskDefinitions": [
{
"type": "cargo",
"required": [
"command"
],
"properties": {
"label": {
"type": "string"
},
"command": {
"type": "string"
},
"args": {
"type": "array"
},
"env": {
"type": "object"
}
}
}
], ],
"properties": { "commands": [
"label": { {
"type": "string" "command": "libsyntax-rust.createFile",
}, "title": "Show Rust syntax tree"
"command": { },
"type": "string" {
}, "command": "libsyntax-rust.syntaxTree",
"args": { "title": "Show Rust syntax tree"
"type": "array" },
}, {
"env": { "command": "libsyntax-rust.extendSelection",
"type": "object" "title": "Rust Extend Selection"
} },
} {
} "command": "libsyntax-rust.matchingBrace",
], "title": "Rust Matching Brace"
"commands": [ },
{ {
"command": "libsyntax-rust.createFile", "command": "libsyntax-rust.parentModule",
"title": "Show Rust syntax tree" "title": "Rust Parent Module"
}, },
{ {
"command": "libsyntax-rust.syntaxTree", "command": "libsyntax-rust.joinLines",
"title": "Show Rust syntax tree" "title": "Rust Join Lines"
}, },
{ {
"command": "libsyntax-rust.extendSelection", "command": "libsyntax-rust.run",
"title": "Rust Extend Selection" "title": "Rust Run"
}, }
{
"command": "libsyntax-rust.matchingBrace",
"title": "Rust Matching Brace"
},
{
"command": "libsyntax-rust.parentModule",
"title": "Rust Parent Module"
},
{
"command": "libsyntax-rust.joinLines",
"title": "Rust Join Lines"
},
{
"command": "libsyntax-rust.run",
"title": "Rust Run"
}
],
"keybindings": [
{
"command": "libsyntax-rust.parentModule",
"key": "ctrl+u",
"when": "editorTextFocus && editorLangId == rust"
},
{
"command": "libsyntax-rust.matchingBrace",
"key": "ctrl+shift+m",
"when": "editorTextFocus && editorLangId == rust"
},
{
"command": "libsyntax-rust.extendSelection",
"key": "ctrl+w",
"when": "editorTextFocus && editorLangId == rust"
},
{
"command": "libsyntax-rust.joinLines",
"key": "ctrl+shift+j",
"when": "editorTextFocus && editorLangId == rust"
}
],
"problemMatchers": [
{
"name": "rustc",
"fileLocation": [
"relative",
"${workspaceRoot}"
], ],
"pattern": [ "keybindings": [
{ {
"regexp": "^(warning|warn|error)(\\[(.*)\\])?: (.*)$", "command": "libsyntax-rust.parentModule",
"severity": 1, "key": "ctrl+u",
"message": 4, "when": "editorTextFocus && editorLangId == rust"
"code": 3 },
}, {
{ "command": "libsyntax-rust.matchingBrace",
"regexp": "^([\\s->=]*(.*):(\\d*):(\\d*)|.*)$", "key": "ctrl+shift+m",
"file": 2, "when": "editorTextFocus && editorLangId == rust"
"line": 3, },
"column": 4 {
}, "command": "libsyntax-rust.extendSelection",
{ "key": "ctrl+w",
"regexp": "^.*$" "when": "editorTextFocus && editorLangId == rust"
}, },
{ {
"regexp": "^([\\s->=]*(.*):(\\d*):(\\d*)|.*)$", "command": "libsyntax-rust.joinLines",
"file": 2, "key": "ctrl+shift+j",
"line": 3, "when": "editorTextFocus && editorLangId == rust"
"column": 4 },
} {
"command": "libsyntax-rust.run",
"key": "ctrl+r",
"when": "editorTextFocus && editorLangId == rust"
}
],
"problemMatchers": [
{
"name": "rustc",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": [
{
"regexp": "^(warning|warn|error)(\\[(.*)\\])?: (.*)$",
"severity": 1,
"message": 4,
"code": 3
},
{
"regexp": "^([\\s->=]*(.*):(\\d*):(\\d*)|.*)$",
"file": 2,
"line": 3,
"column": 4
},
{
"regexp": "^.*$"
},
{
"regexp": "^([\\s->=]*(.*):(\\d*):(\\d*)|.*)$",
"file": 2,
"line": 3,
"column": 4
}
]
}
] ]
} }
]
}
} }

View file

@ -1,6 +1,7 @@
'use strict'; 'use strict';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient' import * as lc from 'vscode-languageclient'
import { DH_UNABLE_TO_CHECK_GENERATOR } from 'constants';
let client: lc.LanguageClient; let client: lc.LanguageClient;
@ -12,10 +13,10 @@ let uris = {
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
let textDocumentContentProvider = new TextDocumentContentProvider() let textDocumentContentProvider = new TextDocumentContentProvider()
let dispose = (disposable) => { let dispose = (disposable: vscode.Disposable) => {
context.subscriptions.push(disposable); context.subscriptions.push(disposable);
} }
let registerCommand = (name, f) => { let registerCommand = (name: string, f: any) => {
dispose(vscode.commands.registerCommand(name, f)) dispose(vscode.commands.registerCommand(name, f))
} }
@ -42,7 +43,7 @@ export function activate(context: vscode.ExtensionContext) {
textDocument: { uri: editor.document.uri.toString() }, textDocument: { uri: editor.document.uri.toString() },
offsets: editor.selections.map((s) => { offsets: editor.selections.map((s) => {
return client.code2ProtocolConverter.asPosition(s.active) return client.code2ProtocolConverter.asPosition(s.active)
}) })
} }
let response = await client.sendRequest<lc.Position[]>("m/findMatchingBrace", request) let response = await client.sendRequest<lc.Position[]>("m/findMatchingBrace", request)
editor.selections = editor.selections.map((sel, idx) => { editor.selections = editor.selections.map((sel, idx) => {
@ -71,8 +72,8 @@ export function activate(context: vscode.ExtensionContext) {
let request: lc.TextDocumentIdentifier = { let request: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString() uri: editor.document.uri.toString()
} }
let response = await client.sendRequest<lc.TextDocumentIdentifier>("m/parentModule", request) let response = await client.sendRequest<lc.Location[]>("m/parentModule", request)
let loc: lc.Location = response[0] let loc = response[0]
if (loc == null) return if (loc == null) return
let uri = client.protocol2CodeConverter.asUri(loc.uri) let uri = client.protocol2CodeConverter.asUri(loc.uri)
let range = client.protocol2CodeConverter.asRange(loc.range) let range = client.protocol2CodeConverter.asRange(loc.range)
@ -82,19 +83,40 @@ export function activate(context: vscode.ExtensionContext) {
e.revealRange(range, vscode.TextEditorRevealType.InCenter) e.revealRange(range, vscode.TextEditorRevealType.InCenter)
}) })
registerCommand('libsyntax-rust.run', async (cmd: ProcessSpec) => { let prevRunnable: RunnableQuickPick | undefined = undefined
let task = createTask(cmd) registerCommand('libsyntax-rust.run', async () => {
await vscode.tasks.executeTask(task) let editor = vscode.window.activeTextEditor
if (editor == null || editor.document.languageId != "rust") return
let textDocument: lc.TextDocumentIdentifier = {
uri: editor.document.uri.toString()
}
let params: RunnablesParams = {
textDocument,
position: client.code2ProtocolConverter.asPosition(editor.selection.active)
}
let runnables = await client.sendRequest<Runnable[]>('m/runnables', params)
let items: RunnableQuickPick[] = []
if (prevRunnable) {
items.push(prevRunnable)
}
for (let r of runnables) {
items.push(new RunnableQuickPick(r))
}
let item = await vscode.window.showQuickPick(items)
if (item) {
item.detail = "last"
prevRunnable = item
let task = createTask(item.runnable)
return await vscode.tasks.executeTask(task)
}
}) })
registerCommand('libsyntax-rust.createFile', async (uri_: string) => { registerCommand('libsyntax-rust.createFile', async (uri_: string) => {
console.log(`uri: ${uri_}`)
let uri = vscode.Uri.parse(uri_) let uri = vscode.Uri.parse(uri_)
let edit = new vscode.WorkspaceEdit() let edit = new vscode.WorkspaceEdit()
edit.createFile(uri) edit.createFile(uri)
await vscode.workspace.applyEdit(edit) await vscode.workspace.applyEdit(edit)
let doc = await vscode.workspace.openTextDocument(uri) let doc = await vscode.workspace.openTextDocument(uri)
await vscode.window.showTextDocument(doc) await vscode.window.showTextDocument(doc)
console.log("Done")
}) })
dispose(vscode.workspace.registerTextDocumentContentProvider( dispose(vscode.workspace.registerTextDocumentContentProvider(
@ -113,13 +135,13 @@ export function activate(context: vscode.ExtensionContext) {
// We need to order this after LS updates, but there's no API for that. // We need to order this after LS updates, but there's no API for that.
// Hence, good old setTimeout. // Hence, good old setTimeout.
function afterLs(f) { function afterLs(f: () => any) {
setTimeout(f, 10) setTimeout(f, 10)
} }
export function deactivate(): Thenable<void> { export function deactivate(): Thenable<void> {
if (!client) { if (!client) {
return undefined; return Promise.resolve();
} }
return client.stop(); return client.stop();
} }
@ -148,7 +170,7 @@ function startServer() {
); );
client.onReady().then(() => { client.onReady().then(() => {
client.onNotification( client.onNotification(
new lc.NotificationType("m/publishDecorations"), "m/publishDecorations",
(params: PublishDecorationsParams) => { (params: PublishDecorationsParams) => {
let editor = vscode.window.visibleTextEditors.find( let editor = vscode.window.visibleTextEditors.find(
(editor) => editor.document.uri.toString() == params.uri (editor) => editor.document.uri.toString() == params.uri
@ -164,11 +186,11 @@ function startServer() {
new lc.RequestType<lc.Position, void, any, any>("m/moveCursor"), new lc.RequestType<lc.Position, void, any, any>("m/moveCursor"),
(params: lc.Position, token: lc.CancellationToken) => { (params: lc.Position, token: lc.CancellationToken) => {
let editor = vscode.window.activeTextEditor; let editor = vscode.window.activeTextEditor;
if (editor == null) return if (!editor) return
if (!editor.selection.isEmpty) return if (!editor.selection.isEmpty) return
let position = client.protocol2CodeConverter.asPosition(params) let position = client.protocol2CodeConverter.asPosition(params)
afterLs(() => { afterLs(() => {
editor.selection = new vscode.Selection(position, position) editor!.selection = new vscode.Selection(position, position)
}) })
} }
) )
@ -200,8 +222,8 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider
} }
const decorations = (() => { const decorations: { [index: string]: vscode.TextEditorDecorationType } = (() => {
const decor = (obj) => vscode.window.createTextEditorDecorationType({ color: obj }) const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj })
return { return {
background: decor("#3F3F3F"), background: decor("#3F3F3F"),
error: vscode.window.createTextEditorDecorationType({ error: vscode.window.createTextEditorDecorationType({
@ -224,24 +246,24 @@ function setHighlights(
editor: vscode.TextEditor, editor: vscode.TextEditor,
highlihgs: Array<Decoration> highlihgs: Array<Decoration>
) { ) {
let byTag = {} let byTag: Map<string, vscode.Range[]> = new Map()
for (let tag in decorations) { for (let tag in decorations) {
byTag[tag] = [] byTag.set(tag, [])
} }
for (let d of highlihgs) { for (let d of highlihgs) {
if (!byTag[d.tag]) { if (!byTag.get(d.tag)) {
console.log(`unknown tag ${d.tag}`) console.log(`unknown tag ${d.tag}`)
continue continue
} }
byTag[d.tag].push( byTag.get(d.tag)!.push(
client.protocol2CodeConverter.asRange(d.range) client.protocol2CodeConverter.asRange(d.range)
) )
} }
for (let tag in byTag) { for (let tag of byTag.keys()) {
let dec = decorations[tag] let dec: vscode.TextEditorDecorationType = decorations[tag]
let ranges = byTag[tag] let ranges = byTag.get(tag)!
editor.setDecorations(dec, ranges) editor.setDecorations(dec, ranges)
} }
} }
@ -276,16 +298,35 @@ interface PublishDecorationsParams {
decorations: Decoration[], decorations: Decoration[],
} }
interface RunnablesParams {
textDocument: lc.TextDocumentIdentifier,
position?: lc.Position,
}
interface Runnable {
range: lc.Range;
label: string;
bin: string;
args: string[];
env: { [index: string]: string },
}
class RunnableQuickPick implements vscode.QuickPickItem {
label: string;
description?: string | undefined;
detail?: string | undefined;
picked?: boolean | undefined;
constructor(public runnable: Runnable) {
this.label = runnable.label
}
}
interface Decoration { interface Decoration {
range: lc.Range, range: lc.Range,
tag: string, tag: string,
} }
interface ProcessSpec {
bin: string;
args: string[];
env: { [key: string]: string };
}
interface CargoTaskDefinition extends vscode.TaskDefinition { interface CargoTaskDefinition extends vscode.TaskDefinition {
type: 'cargo'; type: 'cargo';
@ -295,8 +336,7 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
env?: { [key: string]: string }; env?: { [key: string]: string };
} }
function createTask(spec: Runnable): vscode.Task {
function createTask(spec: ProcessSpec): vscode.Task {
const TASK_SOURCE = 'Rust'; const TASK_SOURCE = 'Rust';
let definition: CargoTaskDefinition = { let definition: CargoTaskDefinition = {
type: 'cargo', type: 'cargo',
@ -313,7 +353,7 @@ function createTask(spec: ProcessSpec): vscode.Task {
}; };
let exec = new vscode.ShellExecution(execCmd, execOption); let exec = new vscode.ShellExecution(execCmd, execOption);
let f = vscode.workspace.workspaceFolders[0] let f = vscode.workspace.workspaceFolders![0]
let t = new vscode.Task(definition, f, definition.label, TASK_SOURCE, exec, ['$rustc']); let t = new vscode.Task(definition, f, definition.label, TASK_SOURCE, exec, ['$rustc']);
return t; return t;
} }

View file

@ -3,9 +3,15 @@
"module": "commonjs", "module": "commonjs",
"target": "es6", "target": "es6",
"outDir": "out", "outDir": "out",
"lib": [ "es6" ], "lib": [
"es6"
],
"sourceMap": true, "sourceMap": true,
"rootDir": "." "rootDir": "src",
"strict": true
}, },
"include": [ "src/*.ts" ], "exclude": [
"node_modules",
".vscode-test"
]
} }

View file

@ -135,48 +135,6 @@ pub fn handle_code_action(
res.push(cmd); res.push(cmd);
} }
} }
for runnable in libeditor::runnables(&file) {
if !contains_offset_nonstrict(runnable.range, offset) {
continue;
}
#[derive(Serialize)]
struct ProcessSpec {
bin: String,
args: Vec<String>,
env: HashMap<String, String>,
}
let spec = ProcessSpec {
bin: "cargo".to_string(),
args: match runnable.kind {
libeditor::RunnableKind::Test { name } => {
vec![
"test".to_string(),
"--".to_string(),
name,
"--nocapture".to_string(),
]
}
libeditor::RunnableKind::Bin => vec!["run".to_string()]
},
env: {
let mut m = HashMap::new();
m.insert(
"RUST_BACKTRACE".to_string(),
"short".to_string(),
);
m
}
};
let cmd = Command {
title: "Run ...".to_string(),
command: "libsyntax-rust.run".to_string(),
arguments: Some(vec![to_value(spec).unwrap()]),
};
res.push(cmd);
}
for (diag, quick_fix) in world.analysis().diagnostics(file_id)? { for (diag, quick_fix) in world.analysis().diagnostics(file_id)? {
let quick_fix = match quick_fix { let quick_fix = match quick_fix {

View file

@ -142,7 +142,7 @@ pub enum Runnables {}
impl Request for Runnables { impl Request for Runnables {
type Params = RunnablesParams; type Params = RunnablesParams;
type Result = Vec<Runnable>; type Result = Vec<Runnable>;
const METHOD: &'static str = "m/joinLines"; const METHOD: &'static str = "m/runnables";
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]

View file

@ -204,7 +204,7 @@ cd code
execute!( execute!(
r" r"
cd code cd code
code --install-extension ./libsyntax-rust-0.0.1.vsix code --install-extension ./rcf-lsp-0.0.1.vsix
" "
)?; )?;
Ok(()) Ok(())