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: recursively parse import files and goto definition of well known types #56

Merged
merged 2 commits into from
Jan 26, 2025
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- ✅ **Diagnostics**: Syntax errors and import error detected with the tree-sitter parser.
- ✅ **Document Symbols**: Navigate and view all symbols, including messages and enums.
- ✅ **Code Formatting**: Format `.proto` files using `clang-format` for a consistent style.
- ✅ **Go to Definition**: Jump to the definition of symbols like messages or enums.
- ✅ **Go to Definition**: Jump to the definition of symbols like messages or enums and imports.
- ✅ **Hover Information**: Get detailed information and documentation on hover.
- ✅ **Rename Symbols**: Rename protobuf symbols and propagate changes across the codebase.
- ✅ **Find References**: Find where messages, enums, and fields are used throughout the codebase.
Expand Down Expand Up @@ -134,7 +134,7 @@ Protols provides a list of symbols in the current document, including nested sym

### Go to Definition

Jump directly to the definition of any custom symbol, including those in other files or packages. This feature works across package boundaries.
Jump directly to the definition of any custom symbol or imports, including those in other files or packages. This feature works across package boundaries.

### Hover Information

Expand Down
11 changes: 6 additions & 5 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ impl LanguageServer for ProtoLanguageServer {
return ControlFlow::Continue(());
};

let Some(diagnostics) = self.state.upsert_file(&uri, content.clone(), &ipath) else {
let Some(diagnostics) = self.state.upsert_file(&uri, content.clone(), &ipath, 8) else {
return ControlFlow::Continue(());
};

Expand All @@ -405,7 +405,7 @@ impl LanguageServer for ProtoLanguageServer {
return ControlFlow::Continue(());
};

let Some(diagnostics) = self.state.upsert_file(&uri, content, &ipath) else {
let Some(diagnostics) = self.state.upsert_file(&uri, content, &ipath, 2) else {
return ControlFlow::Continue(());
};

Expand All @@ -427,9 +427,10 @@ impl LanguageServer for ProtoLanguageServer {
if let Ok(uri) = Url::from_file_path(&file.uri) {
// Safety: The uri is always a file type
let content = read_to_string(uri.to_file_path().unwrap()).unwrap_or_default();
self.state.upsert_content(&uri, content, &[]);
} else {
error!(uri=%file.uri, "failed parse uri");

if let Some(ipath) = self.configs.get_include_paths(&uri) {
self.state.upsert_content(&uri, content, &ipath, 2);
}
}
}
ControlFlow::Continue(())
Expand Down
87 changes: 64 additions & 23 deletions src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
collections::HashMap,
collections::{HashMap, HashSet},
path::PathBuf,
sync::{Arc, Mutex, MutexGuard, RwLock, RwLockWriteGuard},
sync::{Arc, Mutex, RwLock},
};
use tracing::info;

Expand Down Expand Up @@ -66,34 +66,74 @@ impl ProtoLanguageState {
}

fn upsert_content_impl(
mut parser: MutexGuard<ProtoParser>,
&mut self,
uri: &Url,
content: String,
mut docs: RwLockWriteGuard<HashMap<Url, String>>,
mut trees: RwLockWriteGuard<HashMap<Url, ParsedTree>>,
) -> bool {
if let Some(parsed) = parser.parse(uri.clone(), content.as_bytes()) {
trees.insert(uri.clone(), parsed);
docs.insert(uri.clone(), content);
true
} else {
false
ipath: &[PathBuf],
depth: usize,
parse_session: &mut HashSet<Url>,
) {
// Safety: to not cause stack overflow
if depth == 0 {
return;
}

// avoid re-parsing same file incase of circular dependencies
if parse_session.contains(uri) {
return;
}

let Some(parsed) = self
.parser
.lock()
.expect("poison")
.parse(uri.clone(), content.as_bytes())
else {
return;
};

self.trees
.write()
.expect("posion")
.insert(uri.clone(), parsed);

self.documents
.write()
.expect("poison")
.insert(uri.clone(), content.clone());

parse_session.insert(uri.clone());
let imports = self.get_owned_imports(uri, content.as_str());

for import in imports.iter() {
if let Some(p) = ipath.iter().map(|p| p.join(import)).find(|p| p.exists()) {
if let Ok(uri) = Url::from_file_path(p.clone()) {
if let Ok(content) = std::fs::read_to_string(p) {
self.upsert_content_impl(&uri, content, ipath, depth - 1, parse_session);
}
}
}
}
}

fn get_owned_imports(&self, uri: &Url, content: &str) -> Vec<String> {
self.get_tree(uri)
.map(|t| t.get_import_paths(content.as_ref()))
.unwrap_or_default()
.into_iter()
.map(ToOwned::to_owned)
.collect()
}

pub fn upsert_content(
&mut self,
uri: &Url,
content: String,
ipath: &[PathBuf],
depth: usize,
) -> Vec<String> {
// Drop locks at end of block
{
let parser = self.parser.lock().expect("poison");
let tree = self.trees.write().expect("poison");
let docs = self.documents.write().expect("poison");
Self::upsert_content_impl(parser, uri, content.clone(), docs, tree);
}
let mut session = HashSet::new();
self.upsert_content_impl(uri, content.clone(), ipath, depth, &mut session);

// After content is upserted, those imports which couldn't be located
// are flagged as import error
Expand Down Expand Up @@ -189,9 +229,10 @@ impl ProtoLanguageState {
uri: &Url,
content: String,
ipath: &[PathBuf],
depth: usize,
) -> Option<PublishDiagnosticsParams> {
info!(uri=%uri, "upserting file");
let diag = self.upsert_content(uri, content.clone(), ipath);
info!(%uri, %depth, "upserting file");
let diag = self.upsert_content(uri, content.clone(), ipath, depth);
self.get_tree(uri).map(|tree| {
let diag = tree.collect_import_diagnostics(content.as_ref(), diag);
let mut d = tree.collect_parse_diagnostics();
Expand All @@ -205,13 +246,13 @@ impl ProtoLanguageState {
}

pub fn delete_file(&mut self, uri: &Url) {
info!(uri=%uri, "deleting file");
info!(%uri, "deleting file");
self.documents.write().expect("poison").remove(uri);
self.trees.write().expect("poison").remove(uri);
}

pub fn rename_file(&mut self, new_uri: &Url, old_uri: &Url) {
info!(new_uri=%new_uri, old_uri=%new_uri, "renaming file");
info!(%new_uri, %new_uri, "renaming file");

if let Some(v) = self.documents.write().expect("poison").remove(old_uri) {
self.documents
Expand Down
6 changes: 3 additions & 3 deletions src/workspace/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ mod test {
let c = include_str!("input/c.proto");

let mut state: ProtoLanguageState = ProtoLanguageState::new();
state.upsert_file(&a_uri, a.to_owned(), &ipath);
state.upsert_file(&b_uri, b.to_owned(), &ipath);
state.upsert_file(&c_uri, c.to_owned(), &ipath);
state.upsert_file(&a_uri, a.to_owned(), &ipath, 2);
state.upsert_file(&b_uri, b.to_owned(), &ipath, 2);
state.upsert_file(&c_uri, c.to_owned(), &ipath, 2);

assert_yaml_snapshot!(state.definition(
&ipath,
Expand Down
17 changes: 11 additions & 6 deletions src/workspace/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ mod test {

#[test]
fn workspace_test_hover() {
let ipath = vec![PathBuf::from("src/workspace/input")];
let ipath = vec![std::env::current_dir().unwrap().join("src/workspace/input")];
let a_uri = "file://input/a.proto".parse().unwrap();
let b_uri = "file://input/b.proto".parse().unwrap();
let c_uri = "file://input/c.proto".parse().unwrap();
Expand All @@ -682,9 +682,9 @@ mod test {
let c = include_str!("input/c.proto");

let mut state: ProtoLanguageState = ProtoLanguageState::new();
state.upsert_file(&a_uri, a.to_owned(), &ipath);
state.upsert_file(&b_uri, b.to_owned(), &ipath);
state.upsert_file(&c_uri, c.to_owned(), &ipath);
state.upsert_file(&a_uri, a.to_owned(), &ipath, 3);
state.upsert_file(&b_uri, b.to_owned(), &ipath, 2);
state.upsert_file(&c_uri, c.to_owned(), &ipath, 2);

assert_yaml_snapshot!(state.hover(
&ipath,
Expand Down Expand Up @@ -719,7 +719,12 @@ mod test {
assert_yaml_snapshot!(state.hover(
&ipath,
"com.workspace",
Hoverables::ImportPath("c.proto".to_string())
))
Hoverables::Identifier("com.inner.Why".to_string())
));
assert_yaml_snapshot!(state.hover(
&ipath,
"com.workspace",
Hoverables::Identifier("com.super.secret.SomeSecret".to_string())
));
}
}
2 changes: 2 additions & 0 deletions src/workspace/input/a.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ syntax = "proto3";
package com.workspace;

import "c.proto";
import "inner/x.proto";

// A Book is a book
message Book {
Author author = 1;
Author.Address foo = 2;
com.utility.Foobar.Baz z = 3;
com.inner.Why reason = 4;
}

8 changes: 8 additions & 0 deletions src/workspace/input/inner/secret/y.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto3";

package com.super.secret;

// SomeSecret is a real secret with hidden string
message SomeSecret {
string secret = 1;
}
12 changes: 12 additions & 0 deletions src/workspace/input/inner/x.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package com.inner;

import "inner/secret/y.proto";

// Why is a reason with secret
message Why {
string reason = 1;
com.super.secret.SomeSecret secret = 2;
}

12 changes: 6 additions & 6 deletions src/workspace/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ mod test {
let c = include_str!("input/c.proto");

let mut state: ProtoLanguageState = ProtoLanguageState::new();
state.upsert_file(&a_uri, a.to_owned(), &ipath);
state.upsert_file(&b_uri, b.to_owned(), &ipath);
state.upsert_file(&c_uri, c.to_owned(), &ipath);
state.upsert_file(&a_uri, a.to_owned(), &ipath, 2);
state.upsert_file(&b_uri, b.to_owned(), &ipath, 2);
state.upsert_file(&c_uri, c.to_owned(), &ipath, 2);

assert_yaml_snapshot!(state.rename_fields("com.workspace", "Author", "Writer"));
assert_yaml_snapshot!(state.rename_fields(
Expand All @@ -104,9 +104,9 @@ mod test {
let c = include_str!("input/c.proto");

let mut state: ProtoLanguageState = ProtoLanguageState::new();
state.upsert_file(&a_uri, a.to_owned(), &ipath);
state.upsert_file(&b_uri, b.to_owned(), &ipath);
state.upsert_file(&c_uri, c.to_owned(), &ipath);
state.upsert_file(&a_uri, a.to_owned(), &ipath, 2);
state.upsert_file(&b_uri, b.to_owned(), &ipath, 2);
state.upsert_file(&c_uri, c.to_owned(), &ipath, 2);

assert_yaml_snapshot!(state.reference_fields("com.workspace", "Author"));
assert_yaml_snapshot!(state.reference_fields("com.workspace", "Author.Address"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: src/workspace/hover.rs
expression: "state.hover(&ipath, \"com.workspace\",\nHoverables::ImportPath(\"c.proto\".to_string()))"
expression: "state.hover(&ipath, \"com.workspace\",\nHoverables::Identifier(\"com.inner.Why\".to_string()))"
snapshot_kind: text
---
kind: markdown
value: "Import: `c.proto` protobuf file,\n---\nIncluded from src/workspace/input/c.proto"
value: "`Why` message or enum type, package: `com.inner`\n---\nWhy is a reason with secret"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: src/workspace/hover.rs
expression: "state.hover(&ipath, \"com.workspace\",\nHoverables::Identifier(\"com.super.secret.SomeSecret\".to_string()))"
snapshot_kind: text
---
kind: markdown
value: "`SomeSecret` message or enum type, package: `com.super.secret`\n---\nSomeSecret is a real secret with hidden string"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: src/workspace/hover.rs
expression: "state.hover(&ipath, \"com.workspace\",\nHoverables::Identifier(\"com.super.secret.SomeSecret\".to_string()))"
snapshot_kind: text
---
kind: markdown
value: "`SomeSecret` message or enum type, package: `com.super.secret`\n---\nSomeSecret is a real secret with hidden string"
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
source: src/workspace/rename.rs
expression: "state.reference_fields(\"com.workspace\", \"Author.Address\")"
snapshot_kind: text
---
- uri: "file://input/a.proto"
range:
start:
line: 9
line: 10
character: 3
end:
line: 9
line: 10
character: 17
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
source: src/workspace/rename.rs
expression: "state.reference_fields(\"com.utility\", \"Foobar.Baz\")"
snapshot_kind: text
---
- uri: "file://input/a.proto"
range:
start:
line: 10
line: 11
character: 3
end:
line: 10
line: 11
character: 25
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
source: src/workspace/rename.rs
expression: "state.reference_fields(\"com.workspace\", \"Author\")"
snapshot_kind: text
---
- uri: "file://input/a.proto"
range:
start:
line: 8
line: 9
character: 3
end:
line: 8
line: 9
character: 9
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
source: src/workspace/rename.rs
expression: "state.rename_fields(\"com.workspace\", \"Author.Address\", \"Author.Location\")"
snapshot_kind: text
---
"file://input/a.proto":
- range:
start:
line: 9
line: 10
character: 3
end:
line: 9
line: 10
character: 17
newText: Author.Location
Loading
Loading