mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-11-03 13:13:18 +00:00 
			
		
		
		
	Add capabilities tests.
This commit is contained in:
		
							parent
							
								
									e35418ceb9
								
							
						
					
					
						commit
						0fe43a124b
					
				@ -273,8 +273,8 @@ impl Request for HoverRequest {
 | 
			
		||||
pub struct Hover {
 | 
			
		||||
    #[serde(flatten)]
 | 
			
		||||
    pub hover: lsp_types::Hover,
 | 
			
		||||
    #[serde(skip_serializing_if = "Option::is_none")]
 | 
			
		||||
    pub actions: Option<Vec<CommandLinkGroup>>,
 | 
			
		||||
    #[serde(skip_serializing_if = "Vec::is_empty")]
 | 
			
		||||
    pub actions: Vec<CommandLinkGroup>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, PartialEq, Clone, Default, Deserialize, Serialize)]
 | 
			
		||||
 | 
			
		||||
@ -555,7 +555,7 @@ pub fn handle_hover(
 | 
			
		||||
            }),
 | 
			
		||||
            range: Some(range),
 | 
			
		||||
        },
 | 
			
		||||
        actions: Some(prepare_hover_actions(&snap, info.info.actions())),
 | 
			
		||||
        actions: prepare_hover_actions(&snap, info.info.actions()),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    Ok(Some(hover))
 | 
			
		||||
@ -1170,10 +1170,7 @@ fn show_references_command(
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn to_command_link(command: Command, tooltip: String) -> lsp_ext::CommandLink {
 | 
			
		||||
    lsp_ext::CommandLink {
 | 
			
		||||
        tooltip: Some(tooltip),
 | 
			
		||||
        command,
 | 
			
		||||
    }
 | 
			
		||||
    lsp_ext::CommandLink { tooltip: Some(tooltip), command }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn show_impl_command_link(
 | 
			
		||||
 | 
			
		||||
@ -715,3 +715,183 @@ pub fn foo(_input: TokenStream) -> TokenStream {
 | 
			
		||||
    let value = res.get("contents").unwrap().get("value").unwrap().to_string();
 | 
			
		||||
    assert_eq!(value, r#""```rust\nfoo::Bar\n```\n\n```rust\nfn bar()\n```""#)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn test_client_support_hover_actions() {
 | 
			
		||||
    if skip_slow_tests() {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let server = Project::with_fixture(
 | 
			
		||||
        r#"
 | 
			
		||||
//- Cargo.toml
 | 
			
		||||
[package]
 | 
			
		||||
name = "foo"
 | 
			
		||||
version = "0.0.0"
 | 
			
		||||
 | 
			
		||||
//- src/lib.rs
 | 
			
		||||
struct Foo(u32);
 | 
			
		||||
 | 
			
		||||
struct NoImpl(u32);
 | 
			
		||||
 | 
			
		||||
impl Foo {
 | 
			
		||||
    fn new() -> Self {
 | 
			
		||||
        Self(1)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
"#,
 | 
			
		||||
    )
 | 
			
		||||
    .with_config(|config| {
 | 
			
		||||
        config.client_caps.hover_actions = true;
 | 
			
		||||
    })
 | 
			
		||||
    .server();
 | 
			
		||||
 | 
			
		||||
    server.wait_until_workspace_is_loaded();
 | 
			
		||||
 | 
			
		||||
    // has 1 implementation
 | 
			
		||||
    server.request::<HoverRequest>(
 | 
			
		||||
        HoverParams {
 | 
			
		||||
            text_document_position_params: TextDocumentPositionParams::new(
 | 
			
		||||
                server.doc_id("src/lib.rs"),
 | 
			
		||||
                Position::new(0, 9),
 | 
			
		||||
            ),
 | 
			
		||||
            work_done_progress_params: Default::default(),
 | 
			
		||||
        },
 | 
			
		||||
        json!({
 | 
			
		||||
            "actions": [{
 | 
			
		||||
                "commands": [{
 | 
			
		||||
                    "arguments": [
 | 
			
		||||
                      "file:///[..]src/lib.rs",
 | 
			
		||||
                      {
 | 
			
		||||
                        "character": 7,
 | 
			
		||||
                        "line": 0
 | 
			
		||||
                      },
 | 
			
		||||
                      [{
 | 
			
		||||
                          "range": { "end": { "character": 1, "line": 8 }, "start": { "character": 0, "line": 4 } },
 | 
			
		||||
                          "uri": "file:///[..]src/lib.rs"
 | 
			
		||||
                      }]
 | 
			
		||||
                    ],
 | 
			
		||||
                    "command": "rust-analyzer.showReferences",
 | 
			
		||||
                    "title": "1 implementation",
 | 
			
		||||
                    "tooltip": "Go to implementations"
 | 
			
		||||
                }]
 | 
			
		||||
            }],
 | 
			
		||||
            "contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct Foo\n```" },
 | 
			
		||||
            "range": { "end": { "character": 10, "line": 0 }, "start": { "character": 7, "line": 0 } }
 | 
			
		||||
        })
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // no hover
 | 
			
		||||
    server.request::<HoverRequest>(
 | 
			
		||||
        HoverParams {
 | 
			
		||||
            text_document_position_params: TextDocumentPositionParams::new(
 | 
			
		||||
                server.doc_id("src/lib.rs"),
 | 
			
		||||
                Position::new(1, 0),
 | 
			
		||||
            ),
 | 
			
		||||
            work_done_progress_params: Default::default(),
 | 
			
		||||
        },
 | 
			
		||||
        json!(null),
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // no implementations
 | 
			
		||||
    server.request::<HoverRequest>(
 | 
			
		||||
        HoverParams {
 | 
			
		||||
            text_document_position_params: TextDocumentPositionParams::new(
 | 
			
		||||
                server.doc_id("src/lib.rs"),
 | 
			
		||||
                Position::new(2, 12),
 | 
			
		||||
            ),
 | 
			
		||||
            work_done_progress_params: Default::default(),
 | 
			
		||||
        },
 | 
			
		||||
        json!({
 | 
			
		||||
            "actions": [{
 | 
			
		||||
                "commands": [{
 | 
			
		||||
                    "arguments": [
 | 
			
		||||
                      "file:///[..]src/lib.rs",
 | 
			
		||||
                      { "character": 7, "line": 2 },
 | 
			
		||||
                      []
 | 
			
		||||
                    ],
 | 
			
		||||
                    "command": "rust-analyzer.showReferences",
 | 
			
		||||
                    "title": "0 implementations",
 | 
			
		||||
                    "tooltip": "Go to implementations"
 | 
			
		||||
                }]
 | 
			
		||||
            }],
 | 
			
		||||
            "contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct NoImpl\n```" },
 | 
			
		||||
            "range": { "end": { "character": 13, "line": 2 }, "start": { "character": 7, "line": 2 } }
 | 
			
		||||
        })
 | 
			
		||||
    );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[test]
 | 
			
		||||
fn test_client_does_not_support_hover_actions() {
 | 
			
		||||
    if skip_slow_tests() {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let server = Project::with_fixture(
 | 
			
		||||
        r#"
 | 
			
		||||
//- Cargo.toml
 | 
			
		||||
[package]
 | 
			
		||||
name = "foo"
 | 
			
		||||
version = "0.0.0"
 | 
			
		||||
 | 
			
		||||
//- src/lib.rs
 | 
			
		||||
struct Foo(u32);
 | 
			
		||||
 | 
			
		||||
struct NoImpl(u32);
 | 
			
		||||
 | 
			
		||||
impl Foo {
 | 
			
		||||
    fn new() -> Self {
 | 
			
		||||
        Self(1)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
"#,
 | 
			
		||||
    )
 | 
			
		||||
    .with_config(|config| {
 | 
			
		||||
        config.client_caps.hover_actions = false;
 | 
			
		||||
    })
 | 
			
		||||
    .server();
 | 
			
		||||
 | 
			
		||||
    server.wait_until_workspace_is_loaded();
 | 
			
		||||
 | 
			
		||||
    // has 1 implementation
 | 
			
		||||
    server.request::<HoverRequest>(
 | 
			
		||||
        HoverParams {
 | 
			
		||||
            text_document_position_params: TextDocumentPositionParams::new(
 | 
			
		||||
                server.doc_id("src/lib.rs"),
 | 
			
		||||
                Position::new(0, 9),
 | 
			
		||||
            ),
 | 
			
		||||
            work_done_progress_params: Default::default(),
 | 
			
		||||
        },
 | 
			
		||||
        json!({
 | 
			
		||||
            "contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct Foo\n```" },
 | 
			
		||||
            "range": { "end": { "character": 10, "line": 0 }, "start": { "character": 7, "line": 0 } }
 | 
			
		||||
        })
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // no hover
 | 
			
		||||
    server.request::<HoverRequest>(
 | 
			
		||||
        HoverParams {
 | 
			
		||||
            text_document_position_params: TextDocumentPositionParams::new(
 | 
			
		||||
                server.doc_id("src/lib.rs"),
 | 
			
		||||
                Position::new(1, 0),
 | 
			
		||||
            ),
 | 
			
		||||
            work_done_progress_params: Default::default(),
 | 
			
		||||
        },
 | 
			
		||||
        json!(null),
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    // no implementations
 | 
			
		||||
    server.request::<HoverRequest>(
 | 
			
		||||
        HoverParams {
 | 
			
		||||
            text_document_position_params: TextDocumentPositionParams::new(
 | 
			
		||||
                server.doc_id("src/lib.rs"),
 | 
			
		||||
                Position::new(2, 12),
 | 
			
		||||
            ),
 | 
			
		||||
            work_done_progress_params: Default::default(),
 | 
			
		||||
        },
 | 
			
		||||
        json!({
 | 
			
		||||
            "contents": { "kind": "markdown", "value": "```rust\nfoo\n```\n\n```rust\nstruct NoImpl\n```" },
 | 
			
		||||
            "range": { "end": { "character": 13, "line": 2 }, "start": { "character": 7, "line": 2 } }
 | 
			
		||||
        })
 | 
			
		||||
    );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user