stuff
This commit is contained in:
parent
fbcf6508e8
commit
5884109b84
5 changed files with 57 additions and 51 deletions
22
Cargo.lock
generated
22
Cargo.lock
generated
|
@ -54,6 +54,17 @@ version = "1.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
|
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "calculator-lsp"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"lsp-server",
|
||||||
|
"lsp-types",
|
||||||
|
"numbat",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.0.83"
|
version = "1.0.83"
|
||||||
|
@ -390,17 +401,6 @@ dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "qalc-inline"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"anyhow",
|
|
||||||
"lsp-server",
|
|
||||||
"lsp-types",
|
|
||||||
"numbat",
|
|
||||||
"serde_json",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quick-xml"
|
name = "quick-xml"
|
||||||
version = "0.30.0"
|
version = "0.30.0"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "qalc-inline"
|
name = "calculator-lsp"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
13
README.md
Normal file
13
README.md
Normal 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
|
67
src/main.rs
67
src/main.rs
|
@ -1,6 +1,4 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::Read;
|
|
||||||
use std::process::Stdio;
|
|
||||||
|
|
||||||
use lsp_types::{
|
use lsp_types::{
|
||||||
DidChangeTextDocumentParams, Position,
|
DidChangeTextDocumentParams, Position,
|
||||||
|
@ -10,6 +8,9 @@ use lsp_types::{
|
||||||
use lsp_types::{InitializeParams, ServerCapabilities};
|
use lsp_types::{InitializeParams, ServerCapabilities};
|
||||||
use lsp_server::{Connection, Message, Response};
|
use lsp_server::{Connection, Message, Response};
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
|
use numbat::InterpreterResult;
|
||||||
|
use numbat::module_importer::BuiltinModuleImporter;
|
||||||
|
use numbat::resolver::CodeSource;
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
eprintln!("starting generic LSP server");
|
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 docs: HashMap<lsp_types::Url, Vec<String>> = HashMap::new();
|
||||||
let mut context = {
|
let mut context = {
|
||||||
let importer = numbat::module_importer::BuiltinModuleImporter::default();
|
let importer = BuiltinModuleImporter::default();
|
||||||
numbat::Context::new(importer)
|
numbat::Context::new(importer)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -62,44 +63,41 @@ fn main_loop(
|
||||||
for line_number in params.range.start.line..params.range.end.line {
|
for line_number in params.range.start.line..params.range.end.line {
|
||||||
let line = &doc[line_number as usize];
|
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;
|
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 {
|
let label = match result {
|
||||||
Ok((result, status)) if status.is_success() => format!("= {:?}", result),
|
Ok((_, result)) => match result {
|
||||||
_ => panic!(),
|
InterpreterResult::Value(value) => Some(format!("= {}", value)),
|
||||||
// Err(error) => format!("= {}", result.trim()),
|
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")
|
if let Some(label) = label {
|
||||||
// .arg("-t")
|
let hint = InlayHint {
|
||||||
// .arg(&line)
|
position: Position {
|
||||||
// .stdin(Stdio::null())
|
line: line_number,
|
||||||
// .stdout(Stdio::piped())
|
character: line.len() as u32,
|
||||||
// .stderr(Stdio::null())
|
},
|
||||||
// .spawn()?;
|
label: lsp_types::InlayHintLabel::String(label),
|
||||||
|
kind: None,
|
||||||
// let mut result = String::new();
|
text_edits: None,
|
||||||
// qalc.stdout.unwrap().read_to_string(&mut result)?;
|
tooltip: None,
|
||||||
|
data: None,
|
||||||
let hint = InlayHint {
|
padding_left: Some(true),
|
||||||
position: Position {
|
padding_right: None,
|
||||||
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:?}");
|
eprintln!("calculated hints: {hints:?}");
|
||||||
|
@ -131,8 +129,7 @@ fn main_loop(
|
||||||
"textDocument/didClose" => {
|
"textDocument/didClose" => {
|
||||||
let params: DidCloseTextDocumentParams = serde_json::from_value(not.params)?;
|
let params: DidCloseTextDocumentParams = serde_json::from_value(not.params)?;
|
||||||
docs.remove(¶ms.text_document.uri);
|
docs.remove(¶ms.text_document.uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
test.num
4
test.num
|
@ -1,4 +0,0 @@
|
||||||
1 + 1
|
|
||||||
|
|
||||||
|
|
||||||
23984 * 92837438293847293847293847
|
|
Loading…
Reference in a new issue