Skip to content

Commit 5ef94da

Browse files
authored
vscode: add status bar item to show server logs (#572)
also: - add missing docs page for syntax errors - clear diagnostics on file close -- fixes stale diagnostics when switching file type from sql to plain text <img width="284" alt="image" src="https://github.com/user-attachments/assets/891b150b-15cd-4ea4-8336-15abfdf67ee4" />
1 parent 041df21 commit 5ef94da

File tree

6 files changed

+99
-10
lines changed

6 files changed

+99
-10
lines changed

crates/squawk/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Please open an issue at https://github.com/sbdchd/squawk/issues/new with the log
220220
if let Some(subcommand) = opts.cmd {
221221
match subcommand {
222222
Command::Server => {
223-
squawk_server::run_server().context("language server failed")?;
223+
squawk_server::run().context("language server failed")?;
224224
}
225225
Command::UploadToGithub(args) => {
226226
github::check_and_comment_on_pr(

crates/squawk_server/src/lib.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ use log::info;
44
use lsp_server::{Connection, Message, Notification, Response};
55
use lsp_types::{
66
CodeDescription, Diagnostic, DiagnosticSeverity, DidChangeTextDocumentParams,
7-
DidOpenTextDocumentParams, GotoDefinitionParams, GotoDefinitionResponse, InitializeParams,
8-
Location, OneOf, Position, PublishDiagnosticsParams, Range, ServerCapabilities,
9-
TextDocumentSyncCapability, TextDocumentSyncKind, Url,
7+
DidCloseTextDocumentParams, DidOpenTextDocumentParams, GotoDefinitionParams,
8+
GotoDefinitionResponse, InitializeParams, Location, OneOf, Position, PublishDiagnosticsParams,
9+
Range, ServerCapabilities, TextDocumentSyncCapability, TextDocumentSyncKind, Url,
1010
notification::{
11-
DidChangeTextDocument, DidOpenTextDocument, Notification as _, PublishDiagnostics,
11+
DidChangeTextDocument, DidCloseTextDocument, DidOpenTextDocument, Notification as _,
12+
PublishDiagnostics,
1213
},
1314
request::{GotoDefinition, Request},
1415
};
1516
use squawk_linter::Linter;
1617
use squawk_syntax::{Parse, SourceFile};
1718

18-
pub fn run_server() -> Result<()> {
19+
pub fn run() -> Result<()> {
1920
info!("Starting Squawk LSP server");
2021

2122
let (connection, io_threads) = Connection::stdio();
@@ -74,6 +75,8 @@ fn main_loop(connection: Connection, params: serde_json::Value) -> Result<()> {
7475
handle_did_open(&connection, notif)?;
7576
} else if notif.method == DidChangeTextDocument::METHOD {
7677
handle_did_change(&connection, notif)?;
78+
} else if notif.method == DidCloseTextDocument::METHOD {
79+
handle_did_close(&connection, notif)?;
7780
}
7881
}
7982
}
@@ -126,6 +129,28 @@ fn handle_did_change(connection: &Connection, notif: lsp_server::Notification) -
126129
Ok(())
127130
}
128131

132+
fn handle_did_close(connection: &Connection, notif: lsp_server::Notification) -> Result<()> {
133+
let params: DidCloseTextDocumentParams = serde_json::from_value(notif.params)?;
134+
let uri = params.text_document.uri;
135+
136+
let publish_params = PublishDiagnosticsParams {
137+
uri,
138+
diagnostics: vec![],
139+
version: None,
140+
};
141+
142+
let notification = Notification {
143+
method: PublishDiagnostics::METHOD.to_owned(),
144+
params: serde_json::to_value(publish_params)?,
145+
};
146+
147+
connection
148+
.sender
149+
.send(Message::Notification(notification))?;
150+
151+
Ok(())
152+
}
153+
129154
fn lint(connection: &Connection, uri: lsp_types::Url, content: &str, version: i32) -> Result<()> {
130155
let parse: Parse<SourceFile> = SourceFile::parse(content);
131156
let parse_errors = parse.errors();

docs/docs/syntax-error.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
id: syntax-error
3+
title: syntax-error
4+
---
5+
6+
## problem
7+
8+
Squawk encountered invalid syntax when parsing.
9+
10+
## examples
11+
12+
trailing comma
13+
14+
```sql
15+
select f(1,2,);
16+
-- error[syntax-error]: unexpected trailing comma
17+
-- --> stdin:1:13
18+
-- |
19+
-- 1 | select f(1,2,);
20+
-- | ^
21+
-- |
22+
```
23+
24+
missing semicolon
25+
26+
```sql
27+
select * from t
28+
select id from users where email = email;
29+
-- error[syntax-error]: expected SEMICOLON
30+
-- --> stdin:1:16
31+
-- |
32+
-- 1 | select * from t
33+
-- | ^
34+
-- |
35+
```
36+
37+
## solutions
38+
39+
Fix the syntax error.
40+
41+
:::note
42+
Squawk might be mistaken, if you think that's the case, please [open an issue](https://github.com/sbdchd/squawk/issues/new)!
43+
:::

docs/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
"ban-create-domain-with-constraint",
3232
"ban-alter-domain-with-add-constraint",
3333
"ban-truncate-cascade",
34+
"syntax-error",
3435
// xtask:new-rule:error-name
3536
],
3637
},

squawk-vscode/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"command": "squawk.serverVersion",
3737
"title": "Show Server Version",
3838
"category": "Squawk"
39+
},
40+
{
41+
"command": "squawk.showLogs",
42+
"title": "Show Server Logs",
43+
"category": "Squawk"
3944
}
4045
],
4146
"languages": [

squawk-vscode/src/extension.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ export async function activate(context: vscode.ExtensionContext) {
3232
)
3333
context.subscriptions.push(serverVersionCommand)
3434

35+
const showLogsCommand = vscode.commands.registerCommand(
36+
"squawk.showLogs",
37+
() => {
38+
client?.outputChannel?.show()
39+
},
40+
)
41+
context.subscriptions.push(showLogsCommand)
42+
43+
const statusBarItem = vscode.window.createStatusBarItem(
44+
vscode.StatusBarAlignment.Right,
45+
100,
46+
)
47+
statusBarItem.text = "Squawk"
48+
statusBarItem.tooltip = "Click to show Squawk Language Server logs"
49+
statusBarItem.command = "squawk.showLogs"
50+
statusBarItem.show()
51+
context.subscriptions.push(statusBarItem)
52+
3553
await startServer(context)
3654
}
3755

@@ -66,10 +84,7 @@ async function startServer(context: vscode.ExtensionContext) {
6684
}
6785
const serverOptions: ServerOptions = serverExecutable
6886
const clientOptions: LanguageClientOptions = {
69-
documentSelector: [
70-
{ scheme: "file", language: "sql" },
71-
{ scheme: "file", language: "postgres" },
72-
],
87+
documentSelector: [{ language: "sql" }, { language: "postgres" }],
7388
outputChannel: vscode.window.createOutputChannel("Squawk Language Server"),
7489
}
7590
client = new LanguageClient(

0 commit comments

Comments
 (0)