update dependencies
This commit is contained in:
parent
7339d61b1c
commit
e1e87f4dc1
2 changed files with 605 additions and 229 deletions
715
Cargo.lock
generated
715
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
119
src/main.rs
119
src/main.rs
|
@ -1,22 +1,19 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::Error;
|
||||
use lsp_server::{Connection, Message, Response};
|
||||
use lsp_types::{
|
||||
DidChangeTextDocumentParams, Position,
|
||||
TextDocumentSyncCapability, TextDocumentSyncKind,
|
||||
InlayHint, InlayHintParams, DidOpenTextDocumentParams, DidCloseTextDocumentParams,
|
||||
DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams, InlayHint,
|
||||
InlayHintParams, Position, TextDocumentSyncCapability, TextDocumentSyncKind,
|
||||
};
|
||||
use lsp_types::{InitializeParams, ServerCapabilities};
|
||||
use lsp_server::{Connection, Message, Response};
|
||||
use anyhow::Error;
|
||||
use numbat::InterpreterResult;
|
||||
use numbat::module_importer::BuiltinModuleImporter;
|
||||
use numbat::resolver::CodeSource;
|
||||
use numbat::InterpreterResult;
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
eprintln!("starting generic LSP server");
|
||||
|
||||
let (connection, io_threads) = Connection::stdio();
|
||||
|
||||
|
||||
let server_capabilities = serde_json::to_value(ServerCapabilities {
|
||||
text_document_sync: Some(TextDocumentSyncCapability::Kind(TextDocumentSyncKind::FULL)),
|
||||
inlay_hint_provider: Some(lsp_types::OneOf::Left(true)),
|
||||
|
@ -27,58 +24,65 @@ fn main() -> Result<(), Error> {
|
|||
main_loop(connection, initialization_params)?;
|
||||
io_threads.join()?;
|
||||
|
||||
eprintln!("shutting down server");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main_loop(
|
||||
connection: Connection,
|
||||
params: serde_json::Value,
|
||||
) -> Result<(), Error> {
|
||||
fn main_loop(connection: Connection, params: serde_json::Value) -> Result<(), Error> {
|
||||
let _params: InitializeParams = serde_json::from_value(params).unwrap();
|
||||
|
||||
|
||||
let mut docs: HashMap<lsp_types::Url, Vec<String>> = HashMap::new();
|
||||
let mut context = {
|
||||
let importer = BuiltinModuleImporter::default();
|
||||
numbat::Context::new(importer)
|
||||
};
|
||||
|
||||
let mut settings = numbat::InterpreterSettings { print_fn: Box::new(|_| ()) };
|
||||
|
||||
eprintln!("starting example main loop");
|
||||
|
||||
let mut settings = numbat::InterpreterSettings {
|
||||
print_fn: Box::new(|_| ()),
|
||||
};
|
||||
|
||||
let _ = context
|
||||
.interpret_with_settings(&mut settings, "use prelude", CodeSource::Text)
|
||||
.expect("failed to import prelude");
|
||||
|
||||
for msg in &connection.receiver {
|
||||
match msg {
|
||||
Message::Request(req) => {
|
||||
eprintln!("got request: {req:?}");
|
||||
if connection.handle_shutdown(&req)? {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
|
||||
if req.method == "textDocument/inlayHint" {
|
||||
let params: InlayHintParams = serde_json::from_value(req.params)?;
|
||||
let doc = docs.get(¶ms.text_document.uri).unwrap();
|
||||
let mut hints = vec![];
|
||||
|
||||
|
||||
for line_number in params.range.start.line..params.range.end.line {
|
||||
let line = &doc[line_number as usize];
|
||||
|
||||
|
||||
if !line.chars().next().is_some_and(char::is_whitespace) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let result = context.interpret_with_settings(&mut settings, line, CodeSource::Text);
|
||||
let result =
|
||||
context.interpret_with_settings(&mut settings, line, CodeSource::Text);
|
||||
|
||||
let label = match result {
|
||||
Ok((_, result)) => match result {
|
||||
InterpreterResult::Value(value) => Some(format!("= {}", value)),
|
||||
InterpreterResult::Continue => None,
|
||||
InterpreterResult::Exit(_) => None,
|
||||
},
|
||||
Err(numbat::NumbatError::NameResolutionError(err)) => {
|
||||
Some(format!("-> {}", err))
|
||||
}
|
||||
Err(numbat::NumbatError::ResolverError(err)) => {
|
||||
Some(format!("-> {}", err))
|
||||
}
|
||||
Err(numbat::NumbatError::TypeCheckError(err)) => {
|
||||
Some(format!("-> {}", err))
|
||||
}
|
||||
Err(numbat::NumbatError::RuntimeError(err)) => {
|
||||
Some(format!("-> {}", err))
|
||||
}
|
||||
Err(numbat::NumbatError::NameResolutionError(err)) => Some(format!("-> {}", err)),
|
||||
Err(numbat::NumbatError::ResolverError(err)) => Some(format!("-> {}", err)),
|
||||
Err(numbat::NumbatError::TypeCheckError(err)) => Some(format!("-> {}", err)),
|
||||
Err(numbat::NumbatError::RuntimeError(err)) => Some(format!("-> {}", err)),
|
||||
};
|
||||
|
||||
if let Some(label) = label {
|
||||
|
@ -99,40 +103,47 @@ fn main_loop(
|
|||
hints.push(hint);
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("calculated hints: {hints:?}");
|
||||
|
||||
|
||||
let hints = Response {
|
||||
id: req.id,
|
||||
result: Some(serde_json::to_value(hints)?),
|
||||
error: None,
|
||||
};
|
||||
|
||||
|
||||
connection.sender.send(Message::Response(hints))?;
|
||||
}
|
||||
}
|
||||
Message::Response(resp) => {
|
||||
eprintln!("got response: {resp:?}");
|
||||
}
|
||||
Message::Notification(not) => {
|
||||
eprintln!("got notification: {not:?}");
|
||||
|
||||
match not.method.as_str() {
|
||||
"textDocument/didChange" => {
|
||||
let params: DidChangeTextDocumentParams = serde_json::from_value(not.params)?;
|
||||
docs.insert(params.text_document.uri, params.content_changes[0].text.split('\n').map(String::from).collect());
|
||||
}
|
||||
"textDocument/didOpen" => {
|
||||
let params: DidOpenTextDocumentParams = serde_json::from_value(not.params)?;
|
||||
docs.insert(params.text_document.uri, params.text_document.text.split('\n').map(String::from).collect());
|
||||
}
|
||||
"textDocument/didClose" => {
|
||||
let params: DidCloseTextDocumentParams = serde_json::from_value(not.params)?;
|
||||
docs.remove(¶ms.text_document.uri);
|
||||
}
|
||||
_ => {},
|
||||
Message::Response(_) => {}
|
||||
Message::Notification(not) => match not.method.as_str() {
|
||||
"textDocument/didChange" => {
|
||||
let params: DidChangeTextDocumentParams = serde_json::from_value(not.params)?;
|
||||
docs.insert(
|
||||
params.text_document.uri,
|
||||
params.content_changes[0]
|
||||
.text
|
||||
.split('\n')
|
||||
.map(String::from)
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
}
|
||||
"textDocument/didOpen" => {
|
||||
let params: DidOpenTextDocumentParams = serde_json::from_value(not.params)?;
|
||||
docs.insert(
|
||||
params.text_document.uri,
|
||||
params
|
||||
.text_document
|
||||
.text
|
||||
.split('\n')
|
||||
.map(String::from)
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
"textDocument/didClose" => {
|
||||
let params: DidCloseTextDocumentParams = serde_json::from_value(not.params)?;
|
||||
docs.remove(¶ms.text_document.uri);
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
Loading…
Reference in a new issue