JS plugin

This commit is contained in:
Aleksey Kladov 2018-07-30 21:58:49 +03:00
parent 6fc66c4ee6
commit ac0d8c48f7
16 changed files with 3772 additions and 3 deletions

6
code/.gitignore vendored Normal file
View file

@ -0,0 +1,6 @@
target
index.node
artifacts.json
*.vsix
out/*
node_modules/*

4
code/.npmrc Normal file
View file

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

19
code/.vscode/launch.json vendored Normal file
View file

@ -0,0 +1,19 @@
{
// 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
"version": "0.2.0",
"configurations": [
{
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/src/**/*.js" ],
"preLaunchTask": "npm"
},
]
}

10
code/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,10 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": true,
"node_modules": true
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
}
}

31
code/.vscode/tasks.json vendored Normal file
View file

@ -0,0 +1,31 @@
// Available variables which can be used inside of strings.
// ${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",
// we want to run npm
"command": "npm",
// the command is a shell script
"isShellCommand": true,
// show the output window only if unrecognized errors occur.
"showOutput": "silent",
// we run the custom script "compile" as defined in package.json
"args": ["run", "compile", "--loglevel", "silent"],
// 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"
}

278
code/common.ts Normal file
View file

@ -0,0 +1,278 @@
import * as vscode from 'vscode'
import { log } from 'util'
export function createPlugin(
backend,
fileExtension: string,
disposables: vscode.Disposable[],
doHighlighting: boolean = false,
diganosticCollection: vscode.DiagnosticCollection | null = null
) {
let uris = {
syntaxTree: vscode.Uri.parse(`fall-${fileExtension}://syntaxtree`),
metrics: vscode.Uri.parse(`fall-${fileExtension}://metrics`)
}
function updateActiveEditor() {
let editor = vscode.window.activeTextEditor
if (editor == null) return
let file = currentFile()
if (file == null) return
if (doHighlighting) {
setHighlights(editor, file.highlight())
}
if (diganosticCollection != null) {
diganosticCollection.clear()
diganosticCollection.set(
editor.document.uri,
file.diagnostics()
)
}
}
function currentFile(): EditorFile | null {
let editor = vscode.window.activeTextEditor
if (editor == null) return
let doc = editor.document
return getFile(doc)
}
vscode.window.onDidChangeActiveTextEditor(updateActiveEditor)
let cmd = vscode.commands.registerCommand(`fall-${fileExtension}.applyContextAction`, (range, id) => {
let file = currentFile()
if (file == null) return
return file.applyContextAction(range, id)
})
disposables.push(cmd)
return {
getFile: getFile,
showSyntaxTree: () => {
let file = currentFile()
if (file == null) return
return openDoc(uris.syntaxTree)
},
metrics: () => {
let file = currentFile()
if (file == null) return
return openDoc(uris.metrics)
},
extendSelection: () => {
let editor = vscode.window.activeTextEditor
let file = currentFile()
if (editor == null || file == null) return
editor.selections = editor.selections.map((s) => {
let range = file.extendSelection(s)
return new vscode.Selection(range.start, range.end)
})
},
documentSymbolsProvider: new DocumentSymbolProvider(getFile),
documentFormattingEditProvider: new DocumentFormattingEditProvider(getFile),
codeActionProvider: new CodeActionProvider(getFile, fileExtension)
}
}
export interface FileStructureNode {
name: string
range: [number, number]
children: [FileStructureNode]
}
export interface FallDiagnostic {
range: [number, number]
severity: string
message: string
}
export class EditorFile {
backend;
imp;
doc: vscode.TextDocument;
constructor(backend, imp, doc: vscode.TextDocument) {
this.backend = backend
this.imp = imp
this.doc = doc
}
metrics(): string { return this.call("metrics") }
syntaxTree(): string { return this.call("syntaxTree") }
extendSelection(range_: vscode.Range): vscode.Range | null {
let range = fromVsRange(this.doc, range_)
let exp = this.call("extendSelection", range)
if (exp == null) return null
return toVsRange(this.doc, exp)
}
structure(): Array<FileStructureNode> { return this.call("structure") }
reformat(): Array<vscode.TextEdit> {
let edits = this.call("reformat")
return toVsEdits(this.doc, edits)
}
highlight(): Array<[[number, number], string]> { return this.call("highlight") }
diagnostics(): Array<vscode.Diagnostic> {
return this.call("diagnostics").map((d) => {
let range = toVsRange(this.doc, d.range)
let severity = d.severity == "Error"
? vscode.DiagnosticSeverity.Error
: vscode.DiagnosticSeverity.Warning
return new vscode.Diagnostic(range, d.message, severity)
})
}
contextActions(range_: vscode.Range): Array<string> {
let range = fromVsRange(this.doc, range_)
let result = this.call("contextActions", range)
return result
}
applyContextAction(range_: vscode.Range, id: string) {
let range = fromVsRange(this.doc, range_)
let edits = this.call("applyContextAction", range, id)
let editor = vscode.window.activeTextEditor
return editor.edit((builder) => {
for (let op of edits) {
builder.replace(toVsRange(this.doc, op.delete), op.insert)
}
})
}
call(method: string, ...args) {
let result = this.backend[method](this.imp, ...args)
return result
}
}
function documentToFile(backend, fileExtension: string, disposables: vscode.Disposable[], onChange) {
let docs = {}
function update(doc: vscode.TextDocument, file) {
let key = doc.uri.toString()
if (file == null) {
delete docs[key]
} else {
docs[key] = file
}
onChange(doc)
}
function get(doc: vscode.TextDocument) {
return docs[doc.uri.toString()]
}
function isKnownDoc(doc: vscode.TextDocument) {
return doc.fileName.endsWith(`.${fileExtension}`)
}
vscode.workspace.onDidChangeTextDocument((event: vscode.TextDocumentChangeEvent) => {
let doc = event.document
if (!isKnownDoc(event.document)) return
let tree = get(doc)
if (event.contentChanges.length == 1 && tree) {
let edits = event.contentChanges.map((change) => {
let start = doc.offsetAt(change.range.start)
return {
"delete": [start, start + change.rangeLength],
"insert": change.text
}
})
update(doc, backend.edit(tree, edits))
return
}
update(doc, null)
}, null, disposables)
vscode.workspace.onDidOpenTextDocument((doc: vscode.TextDocument) => {
if (!isKnownDoc(doc)) return
update(doc, backend.parse(doc.getText()))
}, null, disposables)
vscode.workspace.onDidCloseTextDocument((doc: vscode.TextDocument) => {
update(doc, null)
}, null, disposables)
return (doc: vscode.TextDocument) => {
if (!isKnownDoc(doc)) return null
if (!get(doc)) {
update(doc, backend.parse(doc.getText()))
}
let imp = get(doc)
return new EditorFile(backend, imp, doc)
}
}
export class DocumentSymbolProvider implements vscode.DocumentSymbolProvider {
getFile: (doc: vscode.TextDocument) => EditorFile | null;
constructor(getFile) {
this.getFile = getFile
}
provideDocumentSymbols(document: vscode.TextDocument, token: vscode.CancellationToken) {
let file = this.getFile(document)
if (file == null) return null
return file.structure().map((node) => {
return new vscode.SymbolInformation(
node.name,
vscode.SymbolKind.Function,
toVsRange(document, node.range),
null,
null
)
})
}
}
export class DocumentFormattingEditProvider implements vscode.DocumentFormattingEditProvider {
getFile: (doc: vscode.TextDocument) => EditorFile | null;
constructor(getFile) { this.getFile = getFile }
provideDocumentFormattingEdits(
document: vscode.TextDocument,
options: vscode.FormattingOptions,
token: vscode.CancellationToken
): vscode.TextEdit[] {
let file = this.getFile(document)
if (file == null) return []
return file.reformat()
}
}
export class CodeActionProvider implements vscode.CodeActionProvider {
fileExtension: string
getFile: (doc: vscode.TextDocument) => EditorFile | null;
constructor(getFile, fileExtension) {
this.getFile = getFile
this.fileExtension = fileExtension
}
provideCodeActions(
document: vscode.TextDocument,
range: vscode.Range,
context: vscode.CodeActionContext,
token: vscode.CancellationToken
): vscode.Command[] {
let file = this.getFile(document)
if (file == null) return
let actions = file.contextActions(range)
return actions.map((id) => {
return {
title: id,
command: `fall-${this.fileExtension}.applyContextAction`,
arguments: [range, id]
}
})
}
}
export function toVsEdits(doc: vscode.TextDocument, edits): Array<vscode.TextEdit> {
return edits.map((op) => vscode.TextEdit.replace(toVsRange(doc, op.delete), op.insert))
}
async function openDoc(uri: vscode.Uri) {
let document = await vscode.workspace.openTextDocument(uri)
vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true)
}

19
code/native/Cargo.toml Normal file
View file

@ -0,0 +1,19 @@
[package]
name = "backend"
version = "0.1.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
license = "MIT"
build = "build.rs"
exclude = ["artifacts.json", "index.node"]
[workspace]
[lib]
name = "backend"
crate-type = ["dylib"]
[build-dependencies]
neon-build = "0.2.0"
[dependencies]
neon = "0.2.0"
libsyntax2 = { path = "../../" }

7
code/native/build.rs Normal file
View file

@ -0,0 +1,7 @@
extern crate neon_build;
fn main() {
neon_build::setup(); // must be called in build.rs
// add project-specific build logic here...
}

80
code/native/src/lib.rs Normal file
View file

@ -0,0 +1,80 @@
#[macro_use]
extern crate neon;
extern crate libsyntax2;
use libsyntax2::{
TextRange,
File,
utils::dump_tree,
SyntaxKind::*,
};
use neon::prelude::*;
pub struct Wrapper {
inner: File,
}
impl Wrapper {
fn highlight(&self) -> Vec<(TextRange, &'static str)> {
let mut res = Vec::new();
self.inner.for_each_node(|node| {
if node.kind() == ERROR {
res.push((node.range(), "error"))
}
});
res
}
}
declare_types! {
/// A class for generating greeting strings.
pub class RustFile for Wrapper {
init(mut cx) {
let text = cx.argument::<JsString>(0)?.value();
Ok(Wrapper {
inner: File::parse(&text)
})
}
method syntaxTree(mut cx) {
let this = cx.this();
let tree = {
let guard = cx.lock();
let wrapper = this.borrow(&guard);
dump_tree(&wrapper.inner.syntax())
};
Ok(cx.string(tree.as_str()).upcast())
}
method highlight(mut cx) {
let this = cx.this();
let highlights = {
let guard = cx.lock();
let wrapper = this.borrow(&guard);
wrapper.highlight()
};
let res = cx.empty_array();
for (i, (range, tag)) in highlights.into_iter().enumerate() {
let start: u32 = range.start().into();
let end: u32 = range.end().into();
let start = cx.number(start);
let end = cx.number(end);
let tag = cx.string(tag);
let hl = cx.empty_array();
hl.set(&mut cx, 0, start)?;
hl.set(&mut cx, 1, end)?;
hl.set(&mut cx, 2, tag)?;
res.set(&mut cx, i as u32, hl)?;
}
Ok(res.upcast())
}
}
}
register_module!(mut cx, {
cx.export_class::<RustFile>("RustFile")
});

3027
code/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

52
code/package.json Normal file
View file

@ -0,0 +1,52 @@
{
"name": "libsyntax-rust",
"displayName": "libsyntax-rust",
"description": "An experimental Rust plugin for VS Code based on libsyntax2",
"license": "MIT",
"repository": "http://github.com/matklad/libsyntax2/",
"version": "0.0.1",
"publisher": "matklad",
"engines": {
"vscode": "^1.18.0"
},
"devDependencies": {
"@types/node": "^6.0.112",
"neon-cli": "^0.2.0",
"typescript": "^2.9.1",
"vsce": "^1.42.0",
"vscode": "^1.1.18"
},
"scripts": {
"vscode:prepublish": "tsc -p ./",
"compile": "tsc -watch -p ./",
"install": "neon build",
"postinstall": "node ./node_modules/vscode/bin/install"
},
"main": "./out/src/main",
"contributes": {
"languages": [
{
"id": "rust",
"extensions": [
".rs"
]
}
],
"commands": [
{
"command": "libsyntax-rust.syntaxTree",
"title": "Show Rust syntax tree"
}
],
"keybindings": [
{
"command": "libsyntax-rust.semanticSelection",
"key": "ctrl+w",
"when": "editorTextFocus && editorLangId == rust"
}
]
},
"activationEvents": [
"onLanguage:rust"
]
}

193
code/src/main.ts Normal file
View file

@ -0,0 +1,193 @@
'use strict'
import * as vscode from 'vscode'
const backend = require("../../native")
let docToSyntax;
let uris = {
syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree')
}
export function activate(context: vscode.ExtensionContext) {
let textDocumentContentProvider = new TextDocumentContentProvider()
let dispose = (disposable) => {
context.subscriptions.push(disposable);
}
let registerCommand = (name, f) => {
dispose(vscode.commands.registerCommand(name, f))
}
docToSyntax = documentToFile(context.subscriptions, () => {
let emitter = textDocumentContentProvider.eventEmitter
emitter.fire(uris.syntaxTree)
let syntax = activeSyntax()
console.log(syntax.highlight());
setHighlights(vscode.window.activeTextEditor, syntax.highlight())
})
dispose(vscode.workspace.registerTextDocumentContentProvider(
'libsyntax-rust',
textDocumentContentProvider
))
registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree))
}
export function deactivate() { }
export class Syntax {
imp;
doc: vscode.TextDocument;
constructor(imp, doc: vscode.TextDocument) {
this.imp = imp
this.doc = doc
}
syntaxTree(): string { return this.imp.syntaxTree() }
highlight(): Array<[number, number, string]> { return this.imp.highlight() }
}
function activeDoc() {
return vscode.window.activeTextEditor.document
}
function activeSyntax(): Syntax {
let doc = activeDoc()
if (doc == null) return null
return docToSyntax(doc)
}
async function openDoc(uri: vscode.Uri) {
let document = await vscode.workspace.openTextDocument(uri)
return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true)
}
function documentToFile(disposables: vscode.Disposable[], onChange) {
let docs = {}
function update(doc: vscode.TextDocument, file) {
let key = doc.uri.toString()
if (file == null) {
delete docs[key]
} else {
docs[key] = file
}
onChange(doc)
}
function get(doc: vscode.TextDocument) {
return docs[doc.uri.toString()]
}
function isKnownDoc(doc: vscode.TextDocument) {
return doc.fileName.endsWith('.rs')
}
function createFile(text: String) {
return new backend.RustFile(text)
}
vscode.workspace.onDidChangeTextDocument((event: vscode.TextDocumentChangeEvent) => {
let doc = event.document
if (!isKnownDoc(event.document)) return
update(doc, null)
}, null, disposables)
vscode.workspace.onDidOpenTextDocument((doc: vscode.TextDocument) => {
if (!isKnownDoc(doc)) return
update(doc, createFile(doc.getText()))
}, null, disposables)
vscode.workspace.onDidCloseTextDocument((doc: vscode.TextDocument) => {
update(doc, null)
}, null, disposables)
return (doc: vscode.TextDocument) => {
if (!isKnownDoc(doc)) return null
if (!get(doc)) {
update(doc, createFile(doc.getText()))
}
let imp = get(doc)
return new Syntax(imp, doc)
}
}
export class TextDocumentContentProvider implements vscode.TextDocumentContentProvider {
public eventEmitter = new vscode.EventEmitter<vscode.Uri>()
public syntaxTree: string = "Not available"
public provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> {
let syntax = activeSyntax()
if (syntax == null) return
if (uri.toString() == uris.syntaxTree.toString()) {
return syntax.syntaxTree()
}
}
get onDidChange(): vscode.Event<vscode.Uri> {
return this.eventEmitter.event
}
}
const decorations = (() => {
const decor = (obj) => vscode.window.createTextEditorDecorationType({ color: obj })
return {
background: decor("#3F3F3F"),
error: vscode.window.createTextEditorDecorationType({
borderColor: "red",
borderStyle: "none none dashed none",
}),
comment: decor("#7F9F7F"),
string: decor("#CC9393"),
keyword: decor("#F0DFAF"),
function: decor("#93E0E3"),
parameter: decor("#94BFF3"),
builtin: decor("#DD6718"),
text: decor("#DCDCCC"),
attribute: decor("#BFEBBF"),
literal: decor("#DFAF8F"),
}
})()
function setHighlights(
editor: vscode.TextEditor,
highlihgs: Array<[number, number, string]>
) {
console.log("setHighlight");
let byTag = {}
for (let tag in decorations) {
byTag[tag] = []
}
for (let [start, end, tag] of highlihgs) {
if (!byTag[tag]) {
console.log(`unknown tag ${tag}`)
continue
}
let range = toVsRange(editor.document, [start, end])
byTag[tag].push(range)
}
for (let tag in byTag) {
let dec = decorations[tag]
let ranges = byTag[tag]
editor.setDecorations(dec, ranges)
}
}
export function toVsRange(doc: vscode.TextDocument, range: [number, number]): vscode.Range {
return new vscode.Range(
doc.positionAt(range[0]),
doc.positionAt(range[1]),
)
}
function fromVsRange(doc: vscode.TextDocument, range: vscode.Range): [number, number] {
return [doc.offsetAt(range.start), doc.offsetAt(range.end)]
}

11
code/tsconfig.json Normal file
View file

@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [ "es6" ],
"sourceMap": true,
"rootDir": "."
},
"include": [ "src/*.ts" ]
}

30
src/ast.rs Normal file
View file

@ -0,0 +1,30 @@
use std::sync::Arc;
use {SyntaxNode, TreeRoot, SyntaxRoot, SyntaxNodeRef};
#[derive(Debug)]
pub struct File<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>
}
impl File<Arc<SyntaxRoot>> {
pub fn parse(text: &str) -> Self {
File { syntax: ::parse(text.to_owned()) }
}
}
impl<R: TreeRoot> File<R> {
pub fn syntax(&self) -> SyntaxNode<R> {
self.syntax.clone()
}
pub fn for_each_node(&self, mut f: impl FnMut(SyntaxNodeRef)) {
let syntax = self.syntax();
let syntax = syntax.borrow();
go(syntax, &mut f);
fn go(syntax: SyntaxNodeRef, f: &mut FnMut(SyntaxNodeRef)) {
f(syntax);
syntax.children().into_iter().for_each(f)
}
}
}

View file

@ -29,12 +29,14 @@ mod syntax_kinds;
mod yellow;
/// Utilities for simple uses of the parser.
pub mod utils;
pub mod ast;
pub use {
lexer::{tokenize, Token},
syntax_kinds::SyntaxKind,
text_unit::{TextRange, TextUnit},
yellow::{SyntaxNode, SyntaxNodeRef},
yellow::{SyntaxNode, SyntaxNodeRef, TreeRoot, SyntaxRoot},
ast::File,
};
pub(crate) use yellow::SyntaxError;

View file

@ -3,10 +3,10 @@ mod green;
mod red;
mod syntax;
pub use self::syntax::{SyntaxNode, SyntaxNodeRef};
pub use self::syntax::{SyntaxNode, SyntaxNodeRef, SyntaxRoot, TreeRoot};
pub(crate) use self::{
builder::GreenBuilder,
green::{GreenNode, GreenNodeBuilder},
red::RedNode,
syntax::{SyntaxError, SyntaxRoot},
syntax::{SyntaxError},
};