Skip to content

Commit

Permalink
feat: add sqruff as a VSCode extension
Browse files Browse the repository at this point in the history
  • Loading branch information
gvozdvmozgu committed Jun 14, 2024
1 parent 6c5ae71 commit b22aebe
Show file tree
Hide file tree
Showing 27 changed files with 7,511 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ jobs:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- uses: dtolnay/rust-toolchain@nightly
- run: cargo r -F codegen-docs >> options.md
- run: cargo r -F codegen-docs > options.md
- uses: stefanzweifel/git-auto-commit-action@v5
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# These are backup files generated by rustfmt
**/*.rs.bk

node_modules
dist
.vscode-test-web
.idea

.DS_Store
.DS_Store
88 changes: 84 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
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"
18 changes: 18 additions & 0 deletions crates/language-server/Cargo.toml
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"
111 changes: 111 additions & 0 deletions crates/language-server/src/lib.rs
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)
}
2 changes: 1 addition & 1 deletion crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ slyce = "0.3.1"
ahash = { version = "0.8.11", features = ["compile-time-rng"] }
stacker = "0.1.15"
lazy-regex = "3.1.0"
anymap = "0.12.1"
anymap = { package = "anymap3", version = "1.0" }
rayon = "1.10.0"
smol_str = "0.2.1"
serde_yaml = "0.9.33"
Expand Down
4 changes: 0 additions & 4 deletions crates/lib/src/core/linter/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use ahash::AHashMap;

use crate::core::config::FluffConfig;
use crate::core::errors::{SQLBaseError, SQLTemplaterError};
use crate::core::parser::segments::base::ErasedSegment;
Expand Down Expand Up @@ -35,7 +33,6 @@ pub struct RenderedFile {
pub templated_file: TemplatedFile,
pub templater_violations: Vec<SQLTemplaterError>,
pub config: FluffConfig,
pub time_dict: AHashMap<&'static str, f64>,
pub(crate) f_name: String,
pub encoding: String,
pub source_str: String,
Expand All @@ -46,7 +43,6 @@ pub struct RenderedFile {
pub struct ParsedString {
pub tree: Option<ErasedSegment>,
pub violations: Vec<SQLBaseError>,
pub time_dict: AHashMap<&'static str, f64>,
pub templated_file: TemplatedFile,
pub f_name: String,
pub source_str: String,
Expand Down
Loading

0 comments on commit b22aebe

Please sign in to comment.