Repository: nushell/nushell
File: crates/nu-lsp/src/workspace.rs (Rust)

Code before:
        "goto/use_module.nu", (0, 10),
        serde_json::json!([make_highlight(0, 4, 0, 13), make_highlight(1, 12, 1, 21)])
    )]
    #[case::module_record_first(
        "workspace/baz.nu", (8, 0),
        serde_json::json!([make_highlight(6, 26, 6, 33), make_highlight(8, 1, 8, 8)])
    )]
    #[case::module_record_second(
        "workspace/baz.nu", (10, 7),
        serde_json::json!([make_highlight(10, 4, 10, 12), make_highlight(11, 1, 11, 8)])
    )]
    fn document_highlight_request(
        #[case] filename: &str,
        #[case] cursor_position: (u32, u32),
        #[case] expected: serde_json::Value,
    ) {
        let mut script = fixtures();
        script.push("lsp");
        script.push(filename);
        let script = path_to_uri(&script);

        let (client_connection, _recv) = initialize_language_server(None, None);
        open_unchecked(&client_connection, script.clone());

        let (line, character) = cursor_position;
        let message = send_document_highlight_request(&client_connection, script, line, character);
        let Message::Response(r) = message else {
            panic!("unexpected message type");
        };
        assert_json_eq!(r.result, expected);
    }
}


Review comment (suggestion):
I find it easier to do a comprehensive test with this document_highlight feature of nu-lsp.

Doesn't bother to mock a `ExportDef`/`ExportExtern` command in [test_parser.rs](https://github.com/nushell/nushell/blob/ce99af9efce2a38ddccd4034ab00bcab379f0f98/crates/nu-parser/tests/test_parser.rs#L4) or expose unmeaningful examples to `ast --help`

Code after:
        "goto/use_module.nu", (0, 10),
        serde_json::json!([make_highlight(0, 4, 0, 13), make_highlight(1, 12, 1, 21)])
    )]
    #[case::module_record_first(
        "workspace/baz.nu", (8, 0),
        serde_json::json!([make_highlight(6, 26, 6, 33), make_highlight(8, 1, 8, 8)])
    )]
    #[case::module_record_second(
        "workspace/baz.nu", (10, 7),
        serde_json::json!([make_highlight(10, 4, 10, 12), make_highlight(11, 1, 11, 8)])
    )]
    // Regression tests for https://github.com/nushell/nushell/pull/16696
    #[case::export_def_with_attributes(
        "workspace/foo.nu", (25, 3),
        serde_json::json!([
            make_highlight(0, 0, 0, 10),
            make_highlight(6, 0, 6, 11),
            make_highlight(10, 4, 10, 14),
            make_highlight(25, 2, 25, 13),
            make_highlight(32, 0, 32, 10),
        ])
    )]
    #[case::export_extern_with_attributes(
        "workspace/foo.nu", (28, 3),
        serde_json::json!([make_highlight(28, 2, 28, 15), make_highlight(35, 0, 35, 13)])
    )]
    fn document_highlight_request(
        #[case] filename: &str,
        #[case] cursor_position: (u32, u32),
        #[case] expected: serde_json::Value,
    ) {
        let mut script = fixtures();
        script.push("lsp");
        script.push(filename);
        let script = path_to_uri(&script);

        let (client_connection, _recv) = initialize_language_server(None, None);
        open_unchecked(&client_connection, script.clone());

        let (line, character) = cursor_position;
        let message = send_document_highlight_request(&client_connection, script, line, character);
        let Message::Response(r) = message else {
            panic!("unexpected message type");
        };
        assert_json_eq!(r.result, expected);
    }
}