Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): pull diagnostics for CSS files #2937

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion crates/biome_lsp/src/handlers/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ pub(crate) fn code_actions(
};

debug!("Cursor range {:?}", &cursor_range);

let result = match session.workspace.pull_actions(PullActionsParams {
path: biome_path.clone(),
range: cursor_range,
Expand Down
132 changes: 109 additions & 23 deletions crates/biome_lsp/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ macro_rules! url {
};
}

fn fixable_diagnostic(line: u32) -> Result<lsp::Diagnostic> {
Ok(lsp::Diagnostic {
range: Range {
start: Position { line, character: 3 },
end: Position {
line,
character: 11,
},
},
severity: Some(lsp::DiagnosticSeverity::ERROR),
code: Some(lsp::NumberOrString::String(String::from(
"lint/suspicious/noCompareNegZero",
))),
code_description: None,
source: Some(String::from("biome")),
message: String::from("Do not use the === operator to compare against -0."),
related_information: None,
tags: None,
data: None,
})
}

struct Server {
service: Timeout<LspService<LSPServer>>,
}
Expand Down Expand Up @@ -150,7 +172,7 @@ impl Server {
InitializeParams {
process_id: None,
root_path: None,
root_uri: Some(url!("/")),
root_uri: Some(url!("")),
initialization_options: None,
capabilities: ClientCapabilities::default(),
trace: None,
Expand Down Expand Up @@ -277,6 +299,8 @@ impl Server {
)
.await
}

/// When calling this function, remember to insert the file inside the memory file system
async fn load_configuration(&mut self) -> Result<()> {
self.notify(
"workspace/didChangeConfiguration",
Expand Down Expand Up @@ -829,28 +853,6 @@ async fn pull_diagnostics_from_new_file() -> Result<()> {
Ok(())
}

fn fixable_diagnostic(line: u32) -> Result<lsp::Diagnostic> {
Ok(lsp::Diagnostic {
range: Range {
start: Position { line, character: 3 },
end: Position {
line,
character: 11,
},
},
severity: Some(lsp::DiagnosticSeverity::ERROR),
code: Some(lsp::NumberOrString::String(String::from(
"lint/suspicious/noCompareNegZero",
))),
code_description: None,
source: Some(String::from("biome")),
message: String::from("Do not use the === operator to compare against -0."),
related_information: None,
tags: None,
data: None,
})
}

#[tokio::test]
async fn pull_quick_fixes() -> Result<()> {
let factory = ServerFactory::default();
Expand Down Expand Up @@ -1382,6 +1384,90 @@ async fn pull_diagnostics_for_rome_json() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn pull_diagnostics_for_css_files() -> Result<()> {
let factory = ServerFactory::default();
let mut fs = MemoryFileSystem::default();
let config = r#"{
"css": {
"linter": { "enabled": true }
},
"linter": {
"rules": { "nursery": { "noUnknownProperty": "error" } }
}
}"#;

fs.insert(url!("biome.json").to_file_path().unwrap(), config);
let (service, client) = factory
.create_with_fs(None, DynRef::Owned(Box::new(fs)))
.into_inner();

let (stream, sink) = client.split();
let mut server = Server::new(service);

let (sender, mut receiver) = channel(CHANNEL_BUFFER_SIZE);
let reader = tokio::spawn(client_handler(stream, sink, sender));

server.initialize().await?;
server.initialized().await?;

server.load_configuration().await?;

let incorrect_config = r#"a {colr: blue;}"#;
server
.open_named_document(incorrect_config, url!("document.css"), "css")
.await?;

let notification = tokio::select! {
msg = receiver.next() => msg,
_ = sleep(Duration::from_secs(1)) => {
panic!("timed out waiting for the server to send diagnostics")
}
};

assert_eq!(
notification,
Some(ServerNotification::PublishDiagnostics(
PublishDiagnosticsParams {
uri: url!("document.css"),
version: Some(0),
diagnostics: vec![lsp::Diagnostic {
range: Range {
start: Position {
line: 0,
character: 3,
},
end: Position {
line: 0,
character: 7,
},
},
severity: Some(lsp::DiagnosticSeverity::ERROR),
code: Some(lsp::NumberOrString::String(String::from(
"lint/nursery/noUnknownProperty"
))),
code_description: Some(CodeDescription {
href: Url::parse("https://biomejs.dev/linter/rules/no-unknown-property")
.unwrap()
}),
source: Some(String::from("biome")),
message: String::from("Unknown property is not allowed.",),
related_information: None,
tags: None,
data: None,
}],
}
))
);

server.close_document().await?;

server.shutdown().await?;
reader.abort();

Ok(())
}

#[tokio::test]
async fn no_code_actions_for_ignored_json_files() -> Result<()> {
let factory = ServerFactory::default();
Expand Down
Loading