Start lang server

This commit is contained in:
Aleksey Kladov 2018-08-10 15:07:43 +03:00
parent 4a900fd681
commit d7c5a6f308
26 changed files with 3258 additions and 94 deletions

View file

@ -5,7 +5,7 @@ authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
license = "MIT OR Apache-2.0"
[workspace]
members = [ "tools", "cli", "libeditor" ]
members = [ "tools", "cli", "libeditor", "libanalysis" ]
[dependencies]
unicode-xid = "0.1.0"
@ -18,4 +18,4 @@ parking_lot = "0.6.0"
testutils = { path = "./tests/testutils" }
[profile.release]
debug = true
debug = true

6
codeless/.gitignore vendored Normal file
View file

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

4
codeless/.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
codeless/.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
codeless/.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
codeless/.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"
}

2381
codeless/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

41
codeless/package.json Normal file
View file

@ -0,0 +1,41 @@
{
"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.25.0"
},
"scripts": {
"compile": "tsc -p ./",
"postinstall": "node ./node_modules/vscode/bin/install"
},
"dependencies": {
"vscode-languageclient": "^4.3.0"
},
"devDependencies": {
"@types/node": "^7.0.56",
"typescript": "^2.9.1",
"vsce": "^1.42.0",
"vscode": "^1.1.18"
},
"main": "./out/src/extension",
"activationEvents": [
"onLanguage:rust"
],
"contributes": {
"commands": [
{
"command": "libsyntax-rust.syntaxTree",
"title": "Show Rust syntax tree"
},
{
"command": "libsyntax-rust.extendSelection",
"title": "Rust Extend Selection"
}
]
}
}

1
codeless/server/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target/*

View file

@ -0,0 +1,16 @@
[package]
name = "m"
version = "0.1.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
[workspace]
[dependencies]
failure = "0.1.2"
languageserver-types = "0.48.0"
serde_json = "1.0.24"
serde = "1.0.71"
serde_derive = "1.0.71"
drop_bomb = "0.1.0"
crossbeam-channel = "0.2.4"
libeditor = { path = "../../libeditor" }
libanalysis = { path = "../../libanalysis" }

View file

@ -0,0 +1,23 @@
use languageserver_types::ServerCapabilities;
pub const SERVER_CAPABILITIES: ServerCapabilities = ServerCapabilities {
text_document_sync: None,
hover_provider: None,
completion_provider: None,
signature_help_provider: None,
definition_provider: None,
type_definition_provider: None,
implementation_provider: None,
references_provider: None,
document_highlight_provider: None,
document_symbol_provider: None,
workspace_symbol_provider: None,
code_action_provider: None,
code_lens_provider: None,
document_formatting_provider: None,
document_range_formatting_provider: None,
document_on_type_formatting_provider: None,
rename_provider: None,
color_provider: None,
execute_command_provider: None,
};

View file

@ -0,0 +1,124 @@
use std::marker::PhantomData;
use serde::{
ser::Serialize,
de::DeserializeOwned,
};
use serde_json;
use drop_bomb::DropBomb;
use ::{
Result,
req::Request,
io::{Io, RawMsg, RawResponse, RawRequest},
};
pub struct Responder<R: Request> {
id: u64,
bomb: DropBomb,
ph: PhantomData<R>,
}
impl<R: Request> Responder<R>
where
R::Params: DeserializeOwned,
R::Result: Serialize,
{
pub fn respond_with(self, io: &mut Io, f: impl FnOnce() -> Result<R::Result>) -> Result<()> {
match f() {
Ok(res) => self.result(io, res)?,
Err(e) => {
self.error(io)?;
return Err(e);
}
}
Ok(())
}
pub fn result(mut self, io: &mut Io, result: R::Result) -> Result<()> {
self.bomb.defuse();
io.send(RawMsg::Response(RawResponse {
id: Some(self.id),
result: serde_json::to_value(result)?,
error: serde_json::Value::Null,
}));
Ok(())
}
pub fn error(mut self, io: &mut Io) -> Result<()> {
self.bomb.defuse();
error(io, self.id, ErrorCode::InternalError, "internal error")
}
}
pub fn parse_as<R>(raw: RawRequest) -> Result<::std::result::Result<(R::Params, Responder<R>), RawRequest>>
where
R: Request,
R::Params: DeserializeOwned,
R::Result: Serialize,
{
if raw.method != R::METHOD {
return Ok(Err(raw));
}
let params: R::Params = serde_json::from_value(raw.params)?;
let responder = Responder {
id: raw.id,
bomb: DropBomb::new("dropped request"),
ph: PhantomData,
};
Ok(Ok((params, responder)))
}
pub fn expect<R>(io: &mut Io, raw: RawRequest) -> Result<Option<(R::Params, Responder<R>)>>
where
R: Request,
R::Params: DeserializeOwned,
R::Result: Serialize,
{
let ret = match parse_as::<R>(raw)? {
Ok(x) => Some(x),
Err(raw) => {
unknown_method(io, raw)?;
None
}
};
Ok(ret)
}
pub fn unknown_method(io: &mut Io, raw: RawRequest) -> Result<()> {
error(io, raw.id, ErrorCode::MethodNotFound, "unknown method")
}
fn error(io: &mut Io, id: u64, code: ErrorCode, message: &'static str) -> Result<()> {
#[derive(Serialize)]
struct Error {
code: i32,
message: &'static str,
}
io.send(RawMsg::Response(RawResponse {
id: Some(id),
result: serde_json::Value::Null,
error: serde_json::to_value(Error {
code: code as i32,
message,
})?,
}));
Ok(())
}
#[allow(unused)]
enum ErrorCode {
ParseError = -32700,
InvalidRequest = -32600,
MethodNotFound = -32601,
InvalidParams = -32602,
InternalError = -32603,
ServerErrorStart = -32099,
ServerErrorEnd = -32000,
ServerNotInitialized = -32002,
UnknownErrorCode = -32001,
RequestCancelled = -32800,
}

201
codeless/server/src/io.rs Normal file
View file

@ -0,0 +1,201 @@
use std::{
thread,
io::{
stdout, stdin,
BufRead, Write,
},
};
use serde_json::{Value, from_str, to_string};
use crossbeam_channel::{Receiver, Sender, bounded};
use Result;
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RawMsg {
Request(RawRequest),
Notification(RawNotification),
Response(RawResponse),
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RawRequest {
pub id: u64,
pub method: String,
pub params: Value,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RawNotification {
pub method: String,
pub params: Value,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct RawResponse {
pub id: Option<u64>,
pub result: Value,
pub error: Value,
}
struct MsgReceiver {
chan: Receiver<RawMsg>,
thread: Option<thread::JoinHandle<Result<()>>>,
}
impl MsgReceiver {
fn recv(&mut self) -> Result<RawMsg> {
match self.chan.recv() {
Some(msg) => Ok(msg),
None => {
self.thread
.take()
.ok_or_else(|| format_err!("MsgReceiver thread panicked"))?
.join()
.map_err(|_| format_err!("MsgReceiver thread panicked"))??;
bail!("client disconnected")
}
}
}
fn stop(self) -> Result<()> {
// Can't really self.thread.join() here, b/c it might be
// blocking on read
Ok(())
}
}
struct MsgSender {
chan: Sender<RawMsg>,
thread: Option<thread::JoinHandle<Result<()>>>,
}
impl MsgSender {
fn send(&mut self, msg: RawMsg) {
self.chan.send(msg)
}
fn stop(mut self) -> Result<()> {
if let Some(thread) = self.thread.take() {
thread.join()
.map_err(|_| format_err!("MsgSender thread panicked"))??
}
Ok(())
}
}
impl Drop for MsgSender {
fn drop(&mut self) {
if let Some(thread) = self.thread.take() {
let res = thread.join();
if thread::panicking() {
drop(res)
} else {
res.unwrap().unwrap()
}
}
}
}
pub struct Io {
receiver: MsgReceiver,
sender: MsgSender,
}
impl Io {
pub fn from_stdio() -> Io {
let sender = {
let (tx, rx) = bounded(16);
MsgSender {
chan: tx,
thread: Some(thread::spawn(move || {
let stdout = stdout();
let mut stdout = stdout.lock();
for msg in rx {
#[derive(Serialize)]
struct JsonRpc {
jsonrpc: &'static str,
#[serde(flatten)]
msg: RawMsg,
}
let text = to_string(&JsonRpc {
jsonrpc: "2.0",
msg,
})?;
write_msg_text(&mut stdout, &text)?;
}
Ok(())
})),
}
};
let receiver = {
let (tx, rx) = bounded(16);
MsgReceiver {
chan: rx,
thread: Some(thread::spawn(move || {
let stdin = stdin();
let mut stdin = stdin.lock();
while let Some(text) = read_msg_text(&mut stdin)? {
let msg: RawMsg = from_str(&text)?;
tx.send(msg);
}
Ok(())
})),
}
};
Io { receiver, sender }
}
pub fn send(&mut self, msg: RawMsg) {
self.sender.send(msg)
}
pub fn recv(&mut self) -> Result<RawMsg> {
self.receiver.recv()
}
pub fn stop(self) -> Result<()> {
self.receiver.stop()?;
self.sender.stop()?;
Ok(())
}
}
fn read_msg_text(inp: &mut impl BufRead) -> Result<Option<String>> {
let mut size = None;
let mut buf = String::new();
loop {
buf.clear();
if inp.read_line(&mut buf)? == 0 {
return Ok(None);
}
if !buf.ends_with("\r\n") {
bail!("malformed header: {:?}", buf);
}
let buf = &buf[..buf.len() - 2];
if buf.is_empty() {
break;
}
let mut parts = buf.splitn(2, ": ");
let header_name = parts.next().unwrap();
let header_value = parts.next().ok_or_else(|| format_err!("malformed header: {:?}", buf))?;
if header_name == "Content-Length" {
size = Some(header_value.parse::<usize>()?);
}
}
let size = size.ok_or_else(|| format_err!("no Content-Length"))?;
let mut buf = buf.into_bytes();
buf.resize(size, 0);
inp.read_exact(&mut buf)?;
let buf = String::from_utf8(buf)?;
Ok(Some(buf))
}
fn write_msg_text(out: &mut impl Write, msg: &str) -> Result<()> {
write!(out, "Content-Length: {}\r\n\r\n", msg.len())?;
out.write_all(msg.as_bytes())?;
out.flush()?;
Ok(())
}

View file

@ -0,0 +1,84 @@
#[macro_use]
extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate languageserver_types;
extern crate drop_bomb;
extern crate crossbeam_channel;
extern crate libeditor;
extern crate libanalysis;
mod io;
mod caps;
mod req;
mod dispatch;
use languageserver_types::InitializeResult;
use libanalysis::WorldState;
use self::io::{Io, RawMsg};
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
fn main() -> Result<()> {
let mut io = Io::from_stdio();
initialize(&mut io)?;
io.stop()?;
Ok(())
}
fn initialize(io: &mut Io) -> Result<()> {
loop {
match io.recv()? {
RawMsg::Request(req) => {
if let Some((_params, resp)) = dispatch::expect::<req::Initialize>(io, req)? {
resp.result(io, InitializeResult {
capabilities: caps::SERVER_CAPABILITIES
})?;
match io.recv()? {
RawMsg::Notification(n) => {
if n.method != "initialized" {
bail!("expected initialized notification");
}
}
_ => {
bail!("expected initialized notification");
}
}
return initialized(io);
}
}
RawMsg::Notification(n) => {
bail!("expected initialize request, got {:?}", n)
}
RawMsg::Response(res) => {
bail!("expected initialize request, got {:?}", res)
}
}
}
}
fn initialized(io: &mut Io) -> Result<()> {
eprintln!("initialized");
let world = WorldState::new();
loop {
match io.recv()? {
RawMsg::Request(req) => {
let world = world.snapshot();
if let Some((params, resp)) = dispatch::expect::<req::SyntaxTree>(io, req)? {
resp.respond_with(io, || {
let path = params.text_document.uri.to_file_path()
.map_err(|()| format_err!("invalid path"))?;
let file = world.file_syntax(&path)?;
Ok(libeditor::syntax_tree(&file))
})?
}
}
msg => {
eprintln!("msg = {:?}", msg);
}
}
}
}

View file

@ -0,0 +1,16 @@
use languageserver_types::TextDocumentIdentifier;
pub use languageserver_types::request::*;
pub enum SyntaxTree {}
impl Request for SyntaxTree {
type Params = SyntaxTreeParams;
type Result = String;
const METHOD: &'static str = "m/syntaxTree";
}
#[derive(Deserialize, Debug)]
#[serde(rename_all="camelCase")]
pub struct SyntaxTreeParams {
pub text_document: TextDocumentIdentifier
}

View file

@ -0,0 +1 @@
{"rustc_fingerprint":11898242945176772229,"outputs":{"15337506775154344876":["___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/matklad/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\nunix\n",""],"1617349019360157463":["___\nlib___.rlib\nlib___.so\nlib___.so\nlib___.a\nlib___.so\n/home/matklad/.rustup/toolchains/stable-x86_64-unknown-linux-gnu\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"gnu\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_os=\"linux\"\ntarget_pointer_width=\"64\"\nunix\n",""],"1164083562126845933":["rustc 1.28.0 (9634041f0 2018-07-30)\nbinary: rustc\ncommit-hash: 9634041f0e8c0f3191d2867311276f19d0a42564\ncommit-date: 2018-07-30\nhost: x86_64-unknown-linux-gnu\nrelease: 1.28.0\nLLVM version: 6.0\n",""]}}

View file

View file

@ -0,0 +1 @@
/home/matklad/projects/libsyntax2/codeless/server/target/debug/libm.rmeta: /home/matklad/projects/libsyntax2/codeless/server/src/caps.rs /home/matklad/projects/libsyntax2/codeless/server/src/dispatch.rs /home/matklad/projects/libsyntax2/codeless/server/src/io.rs /home/matklad/projects/libsyntax2/codeless/server/src/main.rs /home/matklad/projects/libsyntax2/codeless/server/src/req.rs /home/matklad/projects/libsyntax2/libanalysis/src/lib.rs /home/matklad/projects/libsyntax2/libeditor/src/extend_selection.rs /home/matklad/projects/libsyntax2/libeditor/src/lib.rs /home/matklad/projects/libsyntax2/src/algo/mod.rs /home/matklad/projects/libsyntax2/src/algo/walk.rs /home/matklad/projects/libsyntax2/src/ast/generated.rs /home/matklad/projects/libsyntax2/src/ast/mod.rs /home/matklad/projects/libsyntax2/src/grammar/attributes.rs /home/matklad/projects/libsyntax2/src/grammar/expressions/atom.rs /home/matklad/projects/libsyntax2/src/grammar/expressions/mod.rs /home/matklad/projects/libsyntax2/src/grammar/items/consts.rs /home/matklad/projects/libsyntax2/src/grammar/items/mod.rs /home/matklad/projects/libsyntax2/src/grammar/items/structs.rs /home/matklad/projects/libsyntax2/src/grammar/items/traits.rs /home/matklad/projects/libsyntax2/src/grammar/items/use_item.rs /home/matklad/projects/libsyntax2/src/grammar/mod.rs /home/matklad/projects/libsyntax2/src/grammar/params.rs /home/matklad/projects/libsyntax2/src/grammar/paths.rs /home/matklad/projects/libsyntax2/src/grammar/patterns.rs /home/matklad/projects/libsyntax2/src/grammar/type_args.rs /home/matklad/projects/libsyntax2/src/grammar/type_params.rs /home/matklad/projects/libsyntax2/src/grammar/types.rs /home/matklad/projects/libsyntax2/src/lexer/classes.rs /home/matklad/projects/libsyntax2/src/lexer/comments.rs /home/matklad/projects/libsyntax2/src/lexer/mod.rs /home/matklad/projects/libsyntax2/src/lexer/numbers.rs /home/matklad/projects/libsyntax2/src/lexer/ptr.rs /home/matklad/projects/libsyntax2/src/lexer/strings.rs /home/matklad/projects/libsyntax2/src/lib.rs /home/matklad/projects/libsyntax2/src/parser_api.rs /home/matklad/projects/libsyntax2/src/parser_impl/event.rs /home/matklad/projects/libsyntax2/src/parser_impl/input.rs /home/matklad/projects/libsyntax2/src/parser_impl/mod.rs /home/matklad/projects/libsyntax2/src/smol_str.rs /home/matklad/projects/libsyntax2/src/syntax_kinds/generated.rs /home/matklad/projects/libsyntax2/src/syntax_kinds/mod.rs /home/matklad/projects/libsyntax2/src/utils.rs /home/matklad/projects/libsyntax2/src/yellow/builder.rs /home/matklad/projects/libsyntax2/src/yellow/green.rs /home/matklad/projects/libsyntax2/src/yellow/mod.rs /home/matklad/projects/libsyntax2/src/yellow/red.rs /home/matklad/projects/libsyntax2/src/yellow/syntax.rs

View file

88
codeless/src/extension.ts Normal file
View file

@ -0,0 +1,88 @@
'use strict';
import * as vscode from 'vscode';
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind,
Executable,
TextDocumentIdentifier
} from 'vscode-languageclient';
let client: LanguageClient;
let uris = {
syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree')
}
export function activate(context: vscode.ExtensionContext) {
let dispose = (disposable) => {
context.subscriptions.push(disposable);
}
let registerCommand = (name, f) => {
dispose(vscode.commands.registerCommand(name, f))
}
registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree))
dispose(vscode.workspace.registerTextDocumentContentProvider(
'libsyntax-rust',
new TextDocumentContentProvider()
))
startServer()
}
export function deactivate(): Thenable<void> {
if (!client) {
return undefined;
}
return client.stop();
}
function startServer() {
let run: Executable = {
command: "cargo",
args: ["run"],
options: {
cwd: "./server"
}
}
let serverOptions: ServerOptions = {
run,
debug: run
};
let clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'rust' }],
};
client = new LanguageClient(
'm',
'm languge server',
serverOptions,
clientOptions,
);
client.start();
}
async function openDoc(uri: vscode.Uri) {
let document = await vscode.workspace.openTextDocument(uri)
return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true)
}
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 editor = vscode.window.activeTextEditor;
if (editor == null) return ""
let textDocument: TextDocumentIdentifier = { uri: editor.document.uri.toString() };
return client.sendRequest("m/syntaxTree", { textDocument })
}
get onDidChange(): vscode.Event<vscode.Uri> {
return this.eventEmitter.event
}
}

12
codeless/tsconfig.json Normal file
View file

@ -0,0 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "out",
"lib": [ "es6" ],
"sourceMap": true,
"rootDir": "."
},
"include": [ "src" ],
"exclude": [ "node_modules" ]
}

9
libanalysis/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "libanalysis"
version = "0.1.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
[dependencies]
failure = "0.1.2"
parking_lot = "0.6.3"
libsyntax2 = { path = "../" }

107
libanalysis/src/lib.rs Normal file
View file

@ -0,0 +1,107 @@
extern crate failure;
extern crate libsyntax2;
extern crate parking_lot;
use std::{
fs,
sync::Arc,
collections::hash_map::HashMap,
path::{PathBuf, Path},
};
use parking_lot::RwLock;
use libsyntax2::ast;
pub type Result<T> = ::std::result::Result<T, ::failure::Error>;
pub struct WorldState {
data: Arc<WorldData>
}
pub struct World {
data: Arc<WorldData>,
}
impl WorldState {
pub fn new() -> WorldState {
WorldState {
data: Arc::new(WorldData::default())
}
}
pub fn snapshot(&self) -> World {
World { data: self.data.clone() }
}
pub fn change_overlay(&mut self, path: PathBuf, text: Option<String>) {
let data = self.data_mut();
data.file_map.get_mut().remove(&path);
data.fs_map.get_mut().remove(&path);
if let Some(text) = text {
data.mem_map.insert(path, Arc::new(text));
} else {
data.mem_map.remove(&path);
}
}
fn data_mut(&mut self) -> &mut WorldData {
if Arc::get_mut(&mut self.data).is_none() {
let fs_map = self.data.fs_map.read().clone();
let file_map = self.data.file_map.read().clone();
self.data = Arc::new(WorldData {
mem_map: self.data.mem_map.clone(),
fs_map: RwLock::new(fs_map),
file_map: RwLock::new(file_map),
});
}
Arc::get_mut(&mut self.data).unwrap()
}
}
impl World {
pub fn file_syntax(&self, path: &Path) -> Result<ast::File> {
{
let guard = self.data.file_map.read();
if let Some(file) = guard.get(path) {
return Ok(file.clone());
}
}
let file = self.with_file_text(path, ast::File::parse)?;
let mut guard = self.data.file_map.write();
let file = guard.entry(path.to_owned())
.or_insert(file)
.clone();
Ok(file)
}
fn with_file_text<F: FnOnce(&str) -> R, R>(&self, path: &Path, f: F) -> Result<R> {
if let Some(text) = self.data.mem_map.get(path) {
return Ok(f(&*text));
}
{
let guard = self.data.fs_map.read();
if let Some(text) = guard.get(path) {
return Ok(f(&*text));
}
}
let text = fs::read_to_string(path)?;
{
let mut guard = self.data.fs_map.write();
guard.entry(path.to_owned())
.or_insert_with(|| Arc::new(text));
}
let guard = self.data.fs_map.read();
Ok(f(&guard[path]))
}
}
#[derive(Default)]
struct WorldData {
mem_map: HashMap<PathBuf, Arc<String>>,
fs_map: RwLock<HashMap<PathBuf, Arc<String>>>,
file_map: RwLock<HashMap<PathBuf, ast::File>>,
}

View file

@ -6,13 +6,10 @@ use libsyntax2::{
SyntaxNodeRef, AstNode,
algo::walk,
SyntaxKind::*,
ast,
};
pub use libsyntax2::{TextRange, TextUnit};
pub struct File {
inner: libsyntax2::File
}
#[derive(Debug)]
pub struct HighlightedRange {
pub range: TextRange,
@ -44,103 +41,95 @@ pub enum RunnableKind {
Bin,
}
impl File {
pub fn new(text: &str) -> File {
File {
inner: libsyntax2::File::parse(text)
}
pub fn highlight(file: &ast::File) -> Vec<HighlightedRange> {
let syntax = file.syntax();
let mut res = Vec::new();
for node in walk::preorder(syntax.as_ref()) {
let tag = match node.kind() {
ERROR => "error",
COMMENT | DOC_COMMENT => "comment",
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
ATTR => "attribute",
NAME_REF => "text",
NAME => "function",
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
LIFETIME => "parameter",
k if k.is_keyword() => "keyword",
_ => continue,
};
res.push(HighlightedRange {
range: node.range(),
tag,
})
}
res
}
pub fn highlight(&self) -> Vec<HighlightedRange> {
let syntax = self.inner.syntax();
let mut res = Vec::new();
for node in walk::preorder(syntax.as_ref()) {
let tag = match node.kind() {
ERROR => "error",
COMMENT | DOC_COMMENT => "comment",
STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string",
ATTR => "attribute",
NAME_REF => "text",
NAME => "function",
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
LIFETIME => "parameter",
k if k.is_keyword() => "keyword",
_ => continue,
};
res.push(HighlightedRange {
pub fn diagnostics(file: &ast::File) -> Vec<Diagnostic> {
let syntax = file.syntax();
let mut res = Vec::new();
for node in walk::preorder(syntax.as_ref()) {
if node.kind() == ERROR {
res.push(Diagnostic {
range: node.range(),
tag,
})
msg: "Syntax Error".to_string(),
});
}
res
}
res.extend(file.errors().into_iter().map(|err| Diagnostic {
range: TextRange::offset_len(err.offset, 1.into()),
msg: err.msg,
}));
res
}
pub fn diagnostics(&self) -> Vec<Diagnostic> {
let syntax = self.inner.syntax();
let mut res = Vec::new();
pub fn syntax_tree(file: &ast::File) -> String {
::libsyntax2::utils::dump_tree(&file.syntax())
}
for node in walk::preorder(syntax.as_ref()) {
if node.kind() == ERROR {
res.push(Diagnostic {
range: node.range(),
msg: "Syntax Error".to_string(),
});
}
}
res.extend(self.inner.errors().into_iter().map(|err| Diagnostic {
range: TextRange::offset_len(err.offset, 1.into()),
msg: err.msg,
}));
res
}
pub fn symbols(file: &ast::File) -> Vec<Symbol> {
let syntax = file.syntax();
let res: Vec<Symbol> = walk::preorder(syntax.as_ref())
.filter_map(Declaration::cast)
.filter_map(|decl| {
let name = decl.name()?;
let range = decl.range();
Some(Symbol { name, range })
})
.collect();
res // NLL :-(
}
pub fn syntax_tree(&self) -> String {
::libsyntax2::utils::dump_tree(&self.inner.syntax())
}
pub fn extend_selection(file: &ast::File, range: TextRange) -> Option<TextRange> {
let syntax = file.syntax();
extend_selection::extend_selection(syntax.as_ref(), range)
}
pub fn symbols(&self) -> Vec<Symbol> {
let syntax = self.inner.syntax();
let res: Vec<Symbol> = walk::preorder(syntax.as_ref())
.filter_map(Declaration::cast)
.filter_map(|decl| {
let name = decl.name()?;
let range = decl.range();
Some(Symbol { name, range })
pub fn runnables(file: &ast::File) -> Vec<Runnable> {
file
.functions()
.filter_map(|f| {
let name = f.name()?.text();
let kind = if name == "main" {
RunnableKind::Bin
} else if f.has_atom_attr("test") {
RunnableKind::Test {
name: name.to_string()
}
} else {
return None;
};
Some(Runnable {
range: f.syntax().range(),
kind,
})
.collect();
res // NLL :-(
}
pub fn extend_selection(&self, range: TextRange) -> Option<TextRange> {
let syntax = self.inner.syntax();
extend_selection::extend_selection(syntax.as_ref(), range)
}
pub fn runnables(&self) -> Vec<Runnable> {
self.inner
.functions()
.filter_map(|f| {
let name = f.name()?.text();
let kind = if name == "main" {
RunnableKind::Bin
} else if f.has_atom_attr("test") {
RunnableKind::Test {
name: name.to_string()
}
} else {
return None;
};
Some(Runnable {
range: f.syntax().range(),
kind,
})
})
.collect()
}
})
.collect()
}
struct Declaration<'f>(SyntaxNodeRef<'f>);
struct Declaration<'f> (SyntaxNodeRef<'f>);
impl<'f> Declaration<'f> {
fn cast(node: SyntaxNodeRef<'f>) -> Option<Declaration<'f>> {

View file

@ -5,7 +5,7 @@ use {
};
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct File<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>,
}
@ -21,7 +21,7 @@ impl<R: TreeRoot> AstNode<R> for File<R> {
}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Function<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>,
}
@ -37,7 +37,7 @@ impl<R: TreeRoot> AstNode<R> for Function<R> {
}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Name<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>,
}

View file

@ -5,7 +5,7 @@ use {
};
{% for node in ast %}
{% set Name = node.kind | camel %}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct {{ Name }}<R: TreeRoot = Arc<SyntaxRoot>> {
syntax: SyntaxNode<R>,
}