decorations

This commit is contained in:
Aleksey Kladov 2018-08-11 00:55:32 +03:00
parent 836e0c1863
commit 9863b9161d
6 changed files with 143 additions and 27 deletions

View file

@ -1,17 +1,9 @@
'use strict'; 'use strict';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import { import * as lc from 'vscode-languageclient'
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind,
Executable,
TextDocumentIdentifier,
Range
} from 'vscode-languageclient';
let client: LanguageClient; let client: lc.LanguageClient;
let uris = { let uris = {
syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree') syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree')
@ -34,8 +26,7 @@ export function activate(context: vscode.ExtensionContext) {
let request: ExtendSelectionParams = { let request: ExtendSelectionParams = {
textDocument: { uri: editor.document.uri.toString() }, textDocument: { uri: editor.document.uri.toString() },
selections: editor.selections.map((s) => { selections: editor.selections.map((s) => {
let r: Range = { start: s.start, end: s.end } return { start: s.start, end: s.end };
return r;
}) })
} }
let response = await client.sendRequest<ExtendSelectionResult>("m/extendSelection", request) let response = await client.sendRequest<ExtendSelectionResult>("m/extendSelection", request)
@ -71,26 +62,46 @@ export function deactivate(): Thenable<void> {
} }
function startServer() { function startServer() {
let run: Executable = { let run: lc.Executable = {
command: "cargo", command: "cargo",
args: ["run", "--package", "m"], args: ["run", "--package", "m"],
options: { cwd: "." } options: { cwd: "." }
} }
let serverOptions: ServerOptions = { let serverOptions: lc.ServerOptions = {
run, run,
debug: run debug: run
}; };
let clientOptions: LanguageClientOptions = { let clientOptions: lc.LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'rust' }], documentSelector: [{ scheme: 'file', language: 'rust' }],
}; };
client = new LanguageClient( client = new lc.LanguageClient(
'm', 'm',
'm languge server', 'm languge server',
serverOptions, serverOptions,
clientOptions, clientOptions,
); );
client.onReady().then(() => {
client.onNotification(
new lc.NotificationType("m/publishDecorations"),
(params: PublishDecorationsParams) => {
console.log("A");
console.log(params.uri);
console.log(vscode.window.activeTextEditor.document.uri.toString());
console.log("B");
let editor = vscode.window.visibleTextEditors.find(
(editor) => editor.document.uri.toString() == params.uri
)
if (editor == null) return;
setHighlights(
editor,
params.decorations,
)
}
)
})
client.start(); client.start();
} }
@ -117,17 +128,72 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider
} }
} }
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<Decoration>
) {
let byTag = {}
for (let tag in decorations) {
byTag[tag] = []
}
for (let d of highlihgs) {
if (!byTag[d.tag]) {
console.log(`unknown tag ${d.tag}`)
continue
}
byTag[d.tag].push(d.range)
}
for (let tag in byTag) {
let dec = decorations[tag]
let ranges = byTag[tag]
editor.setDecorations(dec, ranges)
}
}
interface SyntaxTreeParams { interface SyntaxTreeParams {
textDocument: TextDocumentIdentifier; textDocument: lc.TextDocumentIdentifier;
} }
type SyntaxTreeResult = string type SyntaxTreeResult = string
interface ExtendSelectionParams { interface ExtendSelectionParams {
textDocument: TextDocumentIdentifier; textDocument: lc.TextDocumentIdentifier;
selections: Range[]; selections: lc.Range[];
} }
interface ExtendSelectionResult { interface ExtendSelectionResult {
selections: Range[]; selections: lc.Range[];
}
interface PublishDecorationsParams {
uri: string,
decorations: Decoration[],
}
interface Decoration {
range: lc.Range,
tag: string,
} }

View file

@ -24,6 +24,7 @@ pub struct WorldState {
data: Arc<WorldData> data: Arc<WorldData>
} }
#[derive(Clone, Debug)]
pub struct World { pub struct World {
data: Arc<WorldData>, data: Arc<WorldData>,
} }
@ -119,12 +120,13 @@ impl World {
} }
#[derive(Default)] #[derive(Default, Debug)]
struct WorldData { struct WorldData {
mem_map: HashMap<PathBuf, Arc<String>>, mem_map: HashMap<PathBuf, Arc<String>>,
file_map: RwLock<HashMap<PathBuf, Arc<FileData>>>, file_map: RwLock<HashMap<PathBuf, Arc<FileData>>>,
} }
#[derive(Debug)]
struct FileData { struct FileData {
text: Option<String>, text: Option<String>,
syntax: OnceCell<ast::File>, syntax: OnceCell<ast::File>,

View file

@ -15,6 +15,7 @@ threadpool = "1.7.1"
flexi_logger = "0.9.0" flexi_logger = "0.9.0"
log = "0.4.3" log = "0.4.3"
url = "1.1.0" url = "1.1.0"
url_serde = "0.2.0"
libeditor = { path = "../libeditor" } libeditor = { path = "../libeditor" }
libanalysis = { path = "../libanalysis" } libanalysis = { path = "../libanalysis" }

View file

@ -4,7 +4,7 @@ use libanalysis::World;
use libeditor::{self, LineIndex, LineCol, TextRange, TextUnit}; use libeditor::{self, LineIndex, LineCol, TextRange, TextUnit};
use ::{ use ::{
req, Result, req::{self, Decoration}, Result,
util::FilePath, util::FilePath,
}; };
@ -51,6 +51,18 @@ pub fn publish_diagnostics(world: World, uri: Url) -> Result<req::PublishDiagnos
Ok(req::PublishDiagnosticsParams { uri, diagnostics }) Ok(req::PublishDiagnosticsParams { uri, diagnostics })
} }
pub fn publish_decorations(world: World, uri: Url) -> Result<req::PublishDecorationsParams> {
let path = uri.file_path()?;
let file = world.file_syntax(&path)?;
let line_index = world.file_line_index(&path)?;
let decorations = libeditor::highlight(&file)
.into_iter()
.map(|h| Decoration {
range: to_vs_range(&line_index, h.range),
tag: h.tag,
}).collect();
Ok(req::PublishDecorationsParams { uri, decorations })
}
fn to_text_range(line_index: &LineIndex, range: Range) -> TextRange { fn to_text_range(line_index: &LineIndex, range: Range) -> TextRange {
TextRange::from_to( TextRange::from_to(

View file

@ -12,6 +12,7 @@ extern crate threadpool;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate url; extern crate url;
extern crate url_serde;
extern crate flexi_logger; extern crate flexi_logger;
extern crate libeditor; extern crate libeditor;
extern crate libanalysis; extern crate libanalysis;
@ -31,7 +32,7 @@ use libanalysis::{WorldState, World};
use ::{ use ::{
io::{Io, RawMsg, RawRequest}, io::{Io, RawMsg, RawRequest},
handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics, publish_decorations},
util::{FilePath, FnBox} util::{FilePath, FnBox}
}; };
@ -198,7 +199,7 @@ fn main_loop(
dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| { dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| {
let path = params.text_document.file_path()?; let path = params.text_document.file_path()?;
world.change_overlay(path, Some(params.text_document.text)); world.change_overlay(path, Some(params.text_document.text));
update_diagnostics_on_threadpool( update_file_notifications_on_threadpool(
pool, world.snapshot(), sender.clone(), params.text_document.uri, pool, world.snapshot(), sender.clone(), params.text_document.uri,
); );
Ok(()) Ok(())
@ -209,7 +210,7 @@ fn main_loop(
.ok_or_else(|| format_err!("empty changes"))? .ok_or_else(|| format_err!("empty changes"))?
.text; .text;
world.change_overlay(path, Some(text)); world.change_overlay(path, Some(text));
update_diagnostics_on_threadpool( update_file_notifications_on_threadpool(
pool, world.snapshot(), sender.clone(), params.text_document.uri, pool, world.snapshot(), sender.clone(), params.text_document.uri,
); );
Ok(()) Ok(())
@ -254,14 +255,14 @@ fn handle_request_on_threadpool<R: req::ClientRequest>(
}) })
} }
fn update_diagnostics_on_threadpool( fn update_file_notifications_on_threadpool(
pool: &ThreadPool, pool: &ThreadPool,
world: World, world: World,
sender: Sender<Thunk>, sender: Sender<Thunk>,
uri: Url, uri: Url,
) { ) {
pool.execute(move || { pool.execute(move || {
match publish_diagnostics(world, uri) { match publish_diagnostics(world.clone(), uri.clone()) {
Err(e) => { Err(e) => {
error!("failed to compute diagnostics: {:?}", e) error!("failed to compute diagnostics: {:?}", e)
} }
@ -271,5 +272,15 @@ fn update_diagnostics_on_threadpool(
})) }))
} }
} }
match publish_decorations(world, uri) {
Err(e) => {
error!("failed to compute decortions: {:?}", e)
}
Ok(params) => {
sender.send(Box::new(|io: &mut Io| {
dispatch::send_notification::<req::PublishDecorations>(io, params)
}))
}
}
}); });
} }

View file

@ -1,5 +1,7 @@
use serde::{ser::Serialize, de::DeserializeOwned}; use serde::{ser::Serialize, de::DeserializeOwned};
use url::Url;
use languageserver_types::{TextDocumentIdentifier, Range}; use languageserver_types::{TextDocumentIdentifier, Range};
use url_serde;
pub use languageserver_types::{ pub use languageserver_types::{
request::*, notification::*, request::*, notification::*,
@ -58,3 +60,25 @@ pub struct ExtendSelectionParams {
pub struct ExtendSelectionResult { pub struct ExtendSelectionResult {
pub selections: Vec<Range>, pub selections: Vec<Range>,
} }
pub enum PublishDecorations {}
impl Notification for PublishDecorations {
type Params = PublishDecorationsParams;
const METHOD: &'static str = "m/publishDecorations";
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PublishDecorationsParams {
#[serde(with = "url_serde")]
pub uri: Url,
pub decorations: Vec<Decoration>,
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Decoration {
pub range: Range,
pub tag: &'static str
}