-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sqruff as a VSCode extension
- Loading branch information
1 parent
6c5ae71
commit 631b9e6
Showing
25 changed files
with
7,434 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
members = ["crates/lib", "crates/cli"] | ||
members = ["crates/lib", "crates/cli", "crates/language-server"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "language-server" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
bench = false | ||
|
||
[dependencies] | ||
js-sys = "0.3.69" | ||
lsp-types = "0.97" | ||
serde = "1.0.203" | ||
serde-wasm-bindgen = "0.6.5" | ||
wasm-bindgen = "0.2" | ||
wasm-bindgen-futures = "0.4.42" | ||
sqruff-lib = { version = "0.7.0", path = "../lib" } | ||
console_error_panic_hook = "0.1.7" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use lsp_types::notification::{DidChangeTextDocument, Notification}; | ||
use lsp_types::{ | ||
Diagnostic, DiagnosticSeverity, DidChangeTextDocumentParams, PublishDiagnosticsParams, | ||
VersionedTextDocumentIdentifier, | ||
}; | ||
use sqruff_lib::core::config::FluffConfig; | ||
use sqruff_lib::core::linter::linter::Linter; | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(js_namespace = console, js_name = log)] | ||
fn console_log(s: &str); | ||
} | ||
|
||
pub(crate) fn log(s: &str) { | ||
#[allow(unused_unsafe)] | ||
unsafe { | ||
console_log(&("[pls] ".to_owned() + s)) | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
pub struct LanguageServer { | ||
send_diagnostics_callback: js_sys::Function, | ||
linter: Linter, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl LanguageServer { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(send_diagnostics_callback: &js_sys::Function) -> Self { | ||
console_error_panic_hook::set_once(); | ||
|
||
Self { | ||
send_diagnostics_callback: send_diagnostics_callback.clone(), | ||
linter: Linter::new(FluffConfig::default(), None, None), | ||
} | ||
} | ||
|
||
#[wasm_bindgen(js_name = onNotification)] | ||
pub fn on_notification(&mut self, method: &str, params: JsValue) { | ||
log(method); | ||
log(&format!("{params:?}")); | ||
|
||
match method { | ||
DidChangeTextDocument::METHOD => { | ||
log("HELLO"); | ||
|
||
let params: DidChangeTextDocumentParams = | ||
serde_wasm_bindgen::from_value(params).unwrap(); | ||
|
||
let content = params.content_changes[0].text.clone(); | ||
let VersionedTextDocumentIdentifier { uri, version } = params.text_document; | ||
|
||
let rule_pack = self.linter.get_rulepack().rules(); | ||
let result = self.linter.lint_string( | ||
content.into(), | ||
None, | ||
false.into(), | ||
None, | ||
None, | ||
rule_pack, | ||
false, | ||
); | ||
|
||
let diagnostics = result | ||
.violations | ||
.into_iter() | ||
.map(|violation| { | ||
Diagnostic::new( | ||
to_range((violation.line_no, violation.line_pos)), | ||
DiagnosticSeverity::WARNING.into(), | ||
None, | ||
None, | ||
violation.description, | ||
None, | ||
None, | ||
) | ||
}) | ||
.collect(); | ||
|
||
self.send_diagnostics(PublishDiagnosticsParams::new( | ||
uri, | ||
diagnostics, | ||
version.into(), | ||
)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
impl LanguageServer { | ||
fn send_diagnostics(&self, diagnostics: PublishDiagnosticsParams) { | ||
let this = &JsValue::null(); | ||
|
||
let diagnostics = &serde_wasm_bindgen::to_value(&diagnostics).unwrap(); | ||
if let Err(e) = self.send_diagnostics_callback.call1(this, diagnostics) { | ||
log(&format!("send_diagnostics params:\n\t{:?}\n\tJS error: {:?}", diagnostics, e)); | ||
} | ||
} | ||
} | ||
|
||
fn to_range(span: (usize, usize)) -> lsp_types::Range { | ||
let pos = lsp_types::Position::new( | ||
(span.0 as u32).saturating_sub(1), | ||
(span.1 as u32).saturating_sub(1), | ||
); | ||
lsp_types::Range::new(pos, pos) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.