Skip to content

Commit 0de56c4

Browse files
committed
Merge branch 'main' into ashar/config
2 parents 0db5ddf + 8ffe219 commit 0de56c4

16 files changed

+600
-86
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "protols"
33
description = "Language server for proto3 files"
4-
version = "0.8.0"
4+
version = "0.9.0"
55
edition = "2021"
66
license = "MIT"
77
homepage = "https://github.com/coder3101/protols"

sample/simple.proto

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ message Book {
1313
google.protobuf.Any data = 4;
1414
BookState state = 5;
1515

16-
// Author is a author of a book
16+
// # Author is a author of a book
17+
// Usage is as follow:
18+
// ```rust
19+
// println!("hello world")
20+
// ```
1721
message Author {
1822
string name = 1;
1923
int64 age = 2;

src/formatter/clang.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct Replacement<'a> {
3838
text: Cow<'a, str>,
3939
}
4040

41-
impl<'a> Replacement<'a> {
41+
impl Replacement<'_> {
4242
fn offset_to_position(offset: usize, content: &str) -> Option<Position> {
4343
if offset > content.len() {
4444
return None;
@@ -81,11 +81,12 @@ impl ClangFormatter {
8181
Some(p)
8282
}
8383

84-
fn get_command(&self, u: &Path) -> Command {
84+
fn get_command(&self, f: &str, u: &Path) -> Option<Command> {
8585
let mut c = Command::new(self.path.as_str());
8686
c.current_dir(self.working_dir.as_str());
87-
c.args([u.to_str().unwrap(), "--output-replacements-xml"]);
88-
c
87+
c.stdin(File::open(u).ok()?);
88+
c.args(["--output-replacements-xml", format!("--assume-filename={f}").as_str()]);
89+
Some(c)
8990
}
9091

9192
fn output_to_textedit(&self, output: &str, content: &str) -> Option<Vec<TextEdit>> {
@@ -101,9 +102,10 @@ impl ClangFormatter {
101102
}
102103

103104
impl ProtoFormatter for ClangFormatter {
104-
fn format_document(&self, content: &str) -> Option<Vec<TextEdit>> {
105+
fn format_document(&self, filename: &str, content: &str) -> Option<Vec<TextEdit>> {
105106
let p = self.get_temp_file_path(content)?;
106-
let output = self.get_command(p.as_ref()).output().ok()?;
107+
let mut cmd = self.get_command(filename, p.as_ref())?;
108+
let output = cmd.output().ok()?;
107109
if !output.status.success() {
108110
tracing::error!(
109111
status = output.status.code(),
@@ -114,12 +116,12 @@ impl ProtoFormatter for ClangFormatter {
114116
self.output_to_textedit(&String::from_utf8_lossy(&output.stdout), content)
115117
}
116118

117-
fn format_document_range(&self, r: &Range, content: &str) -> Option<Vec<TextEdit>> {
119+
fn format_document_range(&self, r: &Range, filename: &str, content: &str) -> Option<Vec<TextEdit>> {
118120
let p = self.get_temp_file_path(content)?;
119121
let start = r.start.line + 1;
120122
let end = r.end.line + 1;
121123
let output = self
122-
.get_command(p.as_ref())
124+
.get_command(filename, p.as_ref())?
123125
.args(["--lines", format!("{start}:{end}").as_str()])
124126
.output()
125127
.ok()?;

src/formatter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ use async_lsp::lsp_types::{Range, TextEdit};
33
pub mod clang;
44

55
pub trait ProtoFormatter: Sized {
6-
fn format_document(&self, content: &str) -> Option<Vec<TextEdit>>;
7-
fn format_document_range(&self, r: &Range, content: &str) -> Option<Vec<TextEdit>>;
6+
fn format_document(&self, filename: &str, content: &str) -> Option<Vec<TextEdit>>;
7+
fn format_document_range(&self, r: &Range, filename: &str, content: &str) -> Option<Vec<TextEdit>>;
88
}

src/lsp.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use async_lsp::lsp_types::{
1313
FileOperationPatternKind, FileOperationRegistrationOptions, GotoDefinitionParams,
1414
GotoDefinitionResponse, Hover, HoverContents, HoverParams, HoverProviderCapability,
1515
InitializeParams, InitializeResult, Location, OneOf, PrepareRenameResponse, ProgressParams,
16-
ReferenceParams, RenameFilesParams, RenameOptions, RenameParams, ServerCapabilities, ServerInfo,
17-
TextDocumentPositionParams, TextDocumentSyncCapability, TextDocumentSyncKind, TextEdit, Url,
18-
WorkspaceEdit, WorkspaceFileOperationsServerCapabilities, WorkspaceFoldersServerCapabilities,
19-
WorkspaceServerCapabilities,
16+
ReferenceParams, RenameFilesParams, RenameOptions, RenameParams, ServerCapabilities,
17+
ServerInfo, TextDocumentPositionParams, TextDocumentSyncCapability, TextDocumentSyncKind,
18+
TextEdit, Url, WorkspaceEdit, WorkspaceFileOperationsServerCapabilities,
19+
WorkspaceFoldersServerCapabilities, WorkspaceServerCapabilities,
2020
};
2121
use async_lsp::{LanguageClient, LanguageServer, ResponseError};
2222
use futures::future::BoxFuture;
@@ -165,23 +165,16 @@ impl LanguageServer for ProtoLanguageServer {
165165
return Box::pin(async move { Ok(None) });
166166
};
167167

168-
let comments = self
168+
let result = self
169169
.state
170170
.hover(current_package_name.as_ref(), identifier.as_ref());
171171

172-
let response = match comments.len() {
173-
0 => None,
174-
1 => Some(Hover {
175-
contents: HoverContents::Scalar(comments[0].clone()),
176-
range: None,
177-
}),
178-
2.. => Some(Hover {
179-
contents: HoverContents::Array(comments),
172+
Box::pin(async move {
173+
Ok(result.map(|r| Hover {
180174
range: None,
181-
}),
182-
};
183-
184-
Box::pin(async move { Ok(response) })
175+
contents: HoverContents::Markup(r),
176+
}))
177+
})
185178
}
186179
fn completion(
187180
&mut self,
@@ -372,7 +365,7 @@ impl LanguageServer for ProtoLanguageServer {
372365
let response = self
373366
.configs
374367
.get_formatter_for_uri(&uri)
375-
.and_then(|f| f.format_document(content.as_str()));
368+
.and_then(|f| f.format_document(uri.path(), content.as_str()));
376369

377370
Box::pin(async move { Ok(response) })
378371
}
@@ -387,7 +380,7 @@ impl LanguageServer for ProtoLanguageServer {
387380
let response = self
388381
.configs
389382
.get_formatter_for_uri(&uri)
390-
.and_then(|f| f.format_document_range(&params.range, content.as_str()));
383+
.and_then(|f| f.format_document_range(&params.range, uri.path(), content.as_str()));
391384

392385
Box::pin(async move { Ok(response) })
393386
}

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@ use server::{ProtoLanguageServer, TickEvent};
99
use tower::ServiceBuilder;
1010
use tracing::Level;
1111

12+
mod formatter;
1213
mod lsp;
1314
mod nodekind;
1415
mod parser;
1516
mod server;
1617
mod state;
1718
mod utils;
1819
mod workspace;
19-
mod formatter;
2020
mod config;
2121

2222
#[tokio::main(flavor = "current_thread")]
2323
async fn main() {
24+
let args: Vec<String> = std::env::args().collect();
25+
if args.len() == 2 && args[1] == "--version" {
26+
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
27+
return;
28+
}
29+
2430
let (server, _) = async_lsp::MainLoop::new_server(|client| {
2531
tokio::spawn({
2632
let client = client.clone();

src/parser/hover.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use async_lsp::lsp_types::MarkedString;
21
use tree_sitter::Node;
32

43
use crate::nodekind::NodeKind;
@@ -47,7 +46,7 @@ impl ParsedTree {
4746
}
4847
}
4948

50-
pub fn hover(&self, identifier: &str, content: impl AsRef<[u8]>) -> Vec<MarkedString> {
49+
pub fn hover(&self, identifier: &str, content: impl AsRef<[u8]>) -> Vec<String> {
5150
let mut results = vec![];
5251
self.hover_impl(identifier, self.tree.root_node(), &mut results, content);
5352
results
@@ -57,7 +56,7 @@ impl ParsedTree {
5756
&self,
5857
identifier: &str,
5958
n: Node,
60-
v: &mut Vec<MarkedString>,
59+
v: &mut Vec<String>,
6160
content: impl AsRef<[u8]>,
6261
) {
6362
if identifier.is_empty() {
@@ -77,14 +76,13 @@ impl ParsedTree {
7776
}
7877
}
7978
None => {
80-
let comments: Vec<MarkedString> = self
79+
let comments: Vec<String> = self
8180
.filter_nodes_from(n, NodeKind::is_userdefined)
8281
.into_iter()
8382
.filter(|n| {
8483
n.utf8_text(content.as_ref()).expect("utf-8 parse error") == identifier
8584
})
8685
.filter_map(|n| self.find_preceding_comments(n.id(), content.as_ref()))
87-
.map(MarkedString::String)
8886
.collect();
8987

9088
v.extend(comments);

src/parser/rename.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl ParsedTree {
119119
.filter(|n| {
120120
let ntext = n.utf8_text(content.as_ref()).expect("utf-8 parse error");
121121
let sc = format!("{old_identifier}.");
122-
return ntext == old_identifier || ntext.starts_with(&sc);
122+
ntext == old_identifier || ntext.starts_with(&sc)
123123
})
124124
.map(|n| {
125125
let text = n.utf8_text(content.as_ref()).expect("utf-8 parse error");

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn split_identifier_package(s: &str) -> (&str, &str) {
5555
});
5656

5757
let (package, identifier) = s.split_at(i);
58-
return (package, identifier.trim_matches('.'));
58+
(package, identifier.trim_matches('.'))
5959
}
6060

6161
#[cfg(test)]

0 commit comments

Comments
 (0)