This commit is contained in:
tezlm 2023-09-26 03:23:25 -07:00
parent fbcf6508e8
commit 5884109b84
Signed by: tezlm
GPG key ID: 649733FCD94AFBBA
5 changed files with 57 additions and 51 deletions

22
Cargo.lock generated
View file

@ -54,6 +54,17 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
[[package]]
name = "calculator-lsp"
version = "0.1.0"
dependencies = [
"anyhow",
"lsp-server",
"lsp-types",
"numbat",
"serde_json",
]
[[package]]
name = "cc"
version = "1.0.83"
@ -390,17 +401,6 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "qalc-inline"
version = "0.1.0"
dependencies = [
"anyhow",
"lsp-server",
"lsp-types",
"numbat",
"serde_json",
]
[[package]]
name = "quick-xml"
version = "0.30.0"

View file

@ -1,5 +1,5 @@
[package]
name = "qalc-inline"
name = "calculator-lsp"
version = "0.1.0"
edition = "2021"

13
README.md Normal file
View file

@ -0,0 +1,13 @@
# calculator lsp
calculate through inlay hints
```
some text here
1 + 2 = 3
```
the lsp will calculate and display the `= 3`
somewhat buggy and somewhat useful, use at your own risk

View file

@ -1,6 +1,4 @@
use std::collections::HashMap;
use std::io::Read;
use std::process::Stdio;
use lsp_types::{
DidChangeTextDocumentParams, Position,
@ -10,6 +8,9 @@ use lsp_types::{
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;
fn main() -> Result<(), Error> {
eprintln!("starting generic LSP server");
@ -38,7 +39,7 @@ fn main_loop(
let mut docs: HashMap<lsp_types::Url, Vec<String>> = HashMap::new();
let mut context = {
let importer = numbat::module_importer::BuiltinModuleImporter::default();
let importer = BuiltinModuleImporter::default();
numbat::Context::new(importer)
};
@ -62,44 +63,41 @@ fn main_loop(
for line_number in params.range.start.line..params.range.end.line {
let line = &doc[line_number as usize];
if line.trim().is_empty() || line.trim().starts_with('#') {
if !line.chars().next().is_some_and(char::is_whitespace) {
continue;
}
let result = context.interpret_with_settings(&mut settings, line, numbat::resolver::CodeSource::Text);
let result = context.interpret_with_settings(&mut settings, line, CodeSource::Text);
let label = match result {
Ok((result, status)) if status.is_success() => format!("= {:?}", result),
_ => panic!(),
// Err(error) => format!("= {}", result.trim()),
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)),
};
// let qalc = std::process::Command::new("qalc")
// .arg("-t")
// .arg(&line)
// .stdin(Stdio::null())
// .stdout(Stdio::piped())
// .stderr(Stdio::null())
// .spawn()?;
// let mut result = String::new();
// qalc.stdout.unwrap().read_to_string(&mut result)?;
let hint = InlayHint {
position: Position {
line: line_number,
character: line.len() as u32,
},
label: lsp_types::InlayHintLabel::String(label),
kind: None,
text_edits: None,
tooltip: None,
data: None,
padding_left: Some(true),
padding_right: None,
};
if let Some(label) = label {
let hint = InlayHint {
position: Position {
line: line_number,
character: line.len() as u32,
},
label: lsp_types::InlayHintLabel::String(label),
kind: None,
text_edits: None,
tooltip: None,
data: None,
padding_left: Some(true),
padding_right: None,
};
hints.push(hint);
hints.push(hint);
}
}
eprintln!("calculated hints: {hints:?}");
@ -131,8 +129,7 @@ fn main_loop(
"textDocument/didClose" => {
let params: DidCloseTextDocumentParams = serde_json::from_value(not.params)?;
docs.remove(&params.text_document.uri);
}
}
_ => {},
}
}

View file

@ -1,4 +0,0 @@
1 + 1
23984 * 92837438293847293847293847