Skip to content

Commit 042b26b

Browse files
committed
Added clap and fixed off-by-one error
1 parent cc734c3 commit 042b26b

File tree

6 files changed

+137
-25
lines changed

6 files changed

+137
-25
lines changed

Cargo.lock

Lines changed: 86 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

harper-core/src/document.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ impl Document {
2323
let tokens = lex_to_end(&source);
2424

2525
let mut doc = Self { source, tokens };
26-
doc.match_quotes();
26+
doc.parse();
2727

2828
doc
2929
}
3030

31+
/// Re-parse important language constructs.
32+
///
33+
/// Should be run after every change to the underlying [`Self::source`].
34+
fn parse(&mut self) {
35+
self.match_quotes();
36+
}
37+
3138
pub fn iter_quote_indices(&self) -> impl Iterator<Item = usize> + '_ {
3239
self.tokens.iter().enumerate().filter_map(|(idx, token)| {
3340
if let TokenKind::Punctuation(Punctuation::Quote(_)) = &token.kind {
@@ -145,6 +152,8 @@ impl Document {
145152
}
146153
}
147154
}
155+
156+
self.parse();
148157
}
149158
}
150159

harper-ls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ serde_json = "1.0.111"
1313
tracing = "0.1.40"
1414
tracing-subscriber = "0.3.18"
1515
harper-core = { path = "../harper-core" }
16+
clap = { version = "4.4.18", features = ["derive"] }

harper-ls/src/generate_diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use harper_core::{all_linters, Dictionary, Document, Lint, Span};
22
use lsp_types::{Diagnostic, Position, Range, Url};
33
use std::fs::read;
44

5-
pub fn generate_diagnostics(file_uri: Url) -> anyhow::Result<Vec<Diagnostic>> {
5+
pub fn generate_diagnostics(file_uri: &Url) -> anyhow::Result<Vec<Diagnostic>> {
66
let file = read(file_uri.path())?;
77
let file_str = String::from_utf8(file)?;
88

@@ -52,7 +52,7 @@ fn index_to_position(source: &[char], index: usize) -> Position {
5252
.collect();
5353

5454
let lines = newline_indices.len();
55-
let cols = index - newline_indices.last().copied().unwrap_or(0);
55+
let cols = index - newline_indices.last().copied().unwrap_or(1) - 1;
5656

5757
Position {
5858
line: lines as u32,

harper-ls/src/main.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
mod generate_diagnostics;
22
mod server;
3-
3+
use clap::Parser;
44
use lsp_server::Connection;
55
use tracing::Level;
66

77
use crate::server::Server;
88

9+
#[derive(Debug, Parser)]
10+
struct Args {
11+
#[arg(short, long, default_value_t = false)]
12+
stdio: bool,
13+
}
14+
915
fn main() -> anyhow::Result<()> {
10-
let subscriber = tracing_subscriber::FmtSubscriber::builder()
11-
.with_max_level(Level::DEBUG)
12-
.finish();
16+
let args = Args::parse();
17+
18+
let (connection, io_threads) = if !args.stdio {
19+
let subscriber = tracing_subscriber::FmtSubscriber::builder()
20+
.with_max_level(Level::DEBUG)
21+
.finish();
22+
23+
tracing::subscriber::set_global_default(subscriber)?;
1324

14-
tracing::subscriber::set_global_default(subscriber)?;
25+
Connection::listen("127.0.0.1:4000")?
26+
} else {
27+
Connection::stdio()
28+
};
1529

16-
let (connection, io_threads) = Connection::stdio();
1730
let mut server = Server::new(connection, io_threads)?;
1831
server.main_loop()?;
1932
server.join()?;

harper-ls/src/server.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use anyhow::Ok;
21
use lsp_server::{
32
Connection, ExtractError, IoThreads, Message, Notification, Request, RequestId, Response,
43
};
54
use lsp_types::{
65
notification::{
7-
DidOpenTextDocument, DidSaveTextDocument, Notification as NotificationTrait,
8-
PublishDiagnostics,
6+
DidChangeTextDocument, DidOpenTextDocument, DidSaveTextDocument,
7+
Notification as NotificationTrait, PublishDiagnostics,
98
},
109
request::GotoDefinition,
11-
Diagnostic, DiagnosticOptions, GotoDefinitionResponse, InitializedParams, Location, Position,
12-
PublishDiagnosticsParams, Range, ServerCapabilities,
10+
CodeActionProviderCapability, Diagnostic, DiagnosticOptions, GotoDefinitionResponse,
11+
InitializedParams, Location, Position, PublishDiagnosticsParams, Range, ServerCapabilities,
12+
Url,
1313
};
1414
use tracing::{error, info};
1515

@@ -30,6 +30,7 @@ impl Server {
3030
diagnostic_provider: Some(lsp_types::DiagnosticServerCapabilities::Options(
3131
DiagnosticOptions::default(),
3232
)),
33+
code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
3334
..Default::default()
3435
})
3536
.unwrap();
@@ -90,21 +91,27 @@ impl Server {
9091
Ok(self.io_threads.join()?)
9192
}
9293

94+
fn handle_save(&self, notif: &Notification) -> anyhow::Result<()> {
95+
let params = cast_notif::<DidSaveTextDocument>(notif.clone())?;
96+
97+
self.publish_diagnostics(&params.text_document.uri)?;
98+
99+
Ok(())
100+
}
101+
93102
fn handle_open(&self, req: &Notification) -> anyhow::Result<()> {
94103
let params = cast_notif::<DidOpenTextDocument>(req.clone())?;
95104

96-
dbg!(params);
105+
self.publish_diagnostics(&params.text_document.uri)?;
97106

98107
Ok(())
99108
}
100109

101-
fn handle_save(&self, req: &Notification) -> anyhow::Result<()> {
102-
let params = cast_notif::<DidSaveTextDocument>(req.clone())?;
103-
104-
let diagnostics = generate_diagnostics(params.text_document.uri.clone())?;
110+
fn publish_diagnostics(&self, uri: &Url) -> anyhow::Result<()> {
111+
let diagnostics = generate_diagnostics(uri)?;
105112

106113
let result = PublishDiagnosticsParams {
107-
uri: params.text_document.uri,
114+
uri: uri.clone(),
108115
diagnostics,
109116
version: None,
110117
};
@@ -137,7 +144,7 @@ impl Server {
137144
},
138145
},
139146
}]));
140-
let result = serde_json::to_value(&result).unwrap();
147+
let result = serde_json::to_value(result).unwrap();
141148
let resp = Response {
142149
id,
143150
result: Some(result),

0 commit comments

Comments
 (0)