~rrc/pbls

3e2d097bfe7e727d50620177618682b523f77f67 — Ryan Roden-Corrent 7 months ago 94bfd54
Fix package identification.
3 files changed, 74 insertions(+), 34 deletions(-)

M src/file.rs
M src/workspace.rs
M tests/integration_test.rs
M src/file.rs => src/file.rs +5 -1
@@ 121,7 121,7 @@ impl File {
    pub fn package(&self) -> Option<&str> {
        static QUERY: OnceLock<tree_sitter::Query> = OnceLock::new();
        let query = QUERY.get_or_init(|| {
            tree_sitter::Query::new(language(), "(package (fullIdent (ident) @id))").unwrap()
            tree_sitter::Query::new(language(), "(package (fullIdent (ident)) @id)").unwrap()
        });

        let mut qc = tree_sitter::QueryCursor::new();


@@ 556,6 556,10 @@ mod tests {
        let file = File::new(text.to_string()).unwrap();
        assert_eq!(file.package(), Some("main".into()));

        let text = r#"syntax="proto3"; package foo.bar.baz;"#;
        let file = File::new(text.to_string()).unwrap();
        assert_eq!(file.package(), Some("foo.bar.baz".into()));

        let text = r#"syntax="proto3";"#;
        let file = File::new(text.to_string()).unwrap();
        assert_eq!(file.package(), None);

M src/workspace.rs => src/workspace.rs +68 -32
@@ 301,6 301,7 @@ impl Workspace {
                file.symbols(&mut qc).find(|sym| sym.name == typ.name)
            } else if let Some(package) = package {
                // different package, fully qualify the name
                log::trace!("Stripping {package} from {}", typ.name);
                let qualified = typ
                    .name
                    .strip_prefix(package)


@@ 308,7 309,7 @@ impl Workspace {
                    .strip_prefix(".")
                    .unwrap_or(typ.name)
                    .to_string();
                log::trace!("Searching for {} in {uri}", qualified);
                log::trace!("Searching for {} in {uri} (different package)", qualified);
                file.symbols(&mut qc).find(|sym| sym.name == qualified)
            } else {
                // target file has no package


@@ 759,21 760,23 @@ mod tests {
                "syntax = \"proto3\";",        // 0
                "package main;",               // 1
                "import \"bar.proto\";",       // 2
                "message One {",               // 3
                "message Two {",               // 4
                "enum Three {}",               // 5
                "}",                           // 6
                "Two.Three tt = 1;",           // 7
                "}",                           // 8
                "message Stuff {",             // 9
                "One one = 1;",                // 10
                "One.Two two = 2;",            // 11
                "One.Two.Three three = 3;",    // 12
                "Two nope = 4;",               // 13
                "bar.One bar_one = 5;",        // 14
                "bar.One.Two b1 = 6;",         // 15
                "bar.One.Two.Three b123 = 7;", // 16
                "}",                           // 17
                "import \"baz.proto\";",       // 3
                "message One {",               // 4
                "message Two {",               // 5
                "enum Three {}",               // 6
                "}",                           // 7
                "Two.Three tt = 1;",           // 8
                "}",                           // 9
                "message Stuff {",             // 10
                "One one = 1;",                // 11
                "One.Two two = 2;",            // 12
                "One.Two.Three three = 3;",    // 13
                "Two nope = 4;",               // 14
                "bar.One bar_one = 5;",        // 15
                "bar.One.Two b1 = 6;",         // 16
                "bar.One.Two.Three b123 = 7;", // 17
                "baz.buz.Baz bazbuz = 8;",     // 18
                "}",                           // 19
            ],
        );
        let (bar_uri, _) = proto(


@@ 791,6 794,15 @@ mod tests {
                "}",                    // 8
            ],
        );
        let (baz_uri, _) = proto(
            &tmp,
            "baz.proto",
            &[
                "syntax = \"proto3\";", // 0
                "package baz.buz;",     // 1
                "message Baz{}",        // 2
            ],
        );

        ws.open(foo_uri.clone(), text).unwrap();



@@ 798,7 810,7 @@ mod tests {
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 7,
                    line: 8,
                    character: "Two.Th".len().try_into().unwrap(),
                }
            )


@@ 807,11 819,11 @@ mod tests {
                uri: foo_uri.clone(),
                range: lsp_types::Range {
                    start: lsp_types::Position {
                        line: 5,
                        line: 6,
                        character: 0,
                    },
                    end: lsp_types::Position {
                        line: 5,
                        line: 6,
                        character: 13,
                    },
                },


@@ 822,7 834,7 @@ mod tests {
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 10,
                    line: 11,
                    character: 0,
                }
            )


@@ 831,11 843,11 @@ mod tests {
                uri: foo_uri.clone(),
                range: lsp_types::Range {
                    start: lsp_types::Position {
                        line: 3,
                        line: 4,
                        character: 0,
                    },
                    end: lsp_types::Position {
                        line: 8,
                        line: 9,
                        character: 1,
                    },
                },


@@ 846,7 858,7 @@ mod tests {
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 11,
                    line: 12,
                    character: "One.".len().try_into().unwrap(),
                }
            )


@@ 855,11 867,11 @@ mod tests {
                uri: foo_uri.clone(),
                range: lsp_types::Range {
                    start: lsp_types::Position {
                        line: 4,
                        line: 5,
                        character: 0,
                    },
                    end: lsp_types::Position {
                        line: 6,
                        line: 7,
                        character: 1,
                    },
                },


@@ 870,7 882,7 @@ mod tests {
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 12,
                    line: 13,
                    character: "One.Two.T".len().try_into().unwrap(),
                }
            )


@@ 879,11 891,11 @@ mod tests {
                uri: foo_uri.clone(),
                range: lsp_types::Range {
                    start: lsp_types::Position {
                        line: 5,
                        line: 6,
                        character: 0,
                    },
                    end: lsp_types::Position {
                        line: 5,
                        line: 6,
                        character: 13,
                    },
                },


@@ 894,7 906,7 @@ mod tests {
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 14,
                    line: 15,
                    character: 0,
                }
            )


@@ 918,7 930,7 @@ mod tests {
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 15,
                    line: 16,
                    character: 0,
                }
            )


@@ 942,7 954,7 @@ mod tests {
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 16,
                    line: 17,
                    character: 0,
                }
            )


@@ 966,7 978,31 @@ mod tests {
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 13,
                    line: 18,
                    character: 2,
                }
            )
            .unwrap(),
            Some(lsp_types::Location {
                uri: baz_uri.clone(),
                range: lsp_types::Range {
                    start: lsp_types::Position {
                        line: 2,
                        character: 0,
                    },
                    end: lsp_types::Position {
                        line: 2,
                        character: 13,
                    },
                },
            })
        );

        assert_eq!(
            ws.goto(
                foo_uri.clone(),
                lsp_types::Position {
                    line: 14,
                    character: 0,
                }
            )

M tests/integration_test.rs => tests/integration_test.rs +1 -1
@@ 836,7 836,7 @@ fn test_complete_type() -> pbls::Result<()> {
            _enum("Dep2"),
            _struct("other.Other"),
            _struct("other.Other.Nested"),
            _struct("folder.Stuff"), // BUG: should be folder.stuff.Stuff
            _struct("folder.stuff.Stuff"),
        ],
        |s| s.label.clone(),
    );