Skip to content

Commit b0ad492

Browse files
bors[bot]Anatol Liu
andauthored
Merge #6519
6519: Add "Open Cargo.toml" action r=matklad a=p3achyjr ## What is it? This adds an "open cargo.toml" action from the vscode shell, resolves #6462 ## Test Ran ```cargo xtask install --server``` and ```cargo xtask install --client```, then ```Developer: Reload Window```. ![image](https://user-images.githubusercontent.com/8763808/98614382-2a578f00-22ad-11eb-9811-4a72a54ff6fb.png) When clicked: ![image](https://user-images.githubusercontent.com/8763808/98618176-77d7fa00-22b5-11eb-8788-35256542f3a6.png) Co-authored-by: Anatol Liu <[email protected]>
2 parents 475a296 + b1b7727 commit b0ad492

File tree

11 files changed

+105
-8
lines changed

11 files changed

+105
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ crates/*/target
1010
generated_assists.adoc
1111
generated_features.adoc
1212
generated_diagnostic.adoc
13+
.DS_Store

crates/ide/.DS_Store

6 KB
Binary file not shown.

crates/rust-analyzer/src/config.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,10 @@ impl Config {
398398
}
399399

400400
if let Some(code_action) = &doc_caps.code_action {
401-
match (code_action.data_support, &code_action.resolve_support) {
402-
(Some(true), Some(resolve_support)) => {
403-
if resolve_support.properties.iter().any(|it| it == "edit") {
404-
self.client_caps.code_action_resolve = true;
405-
}
401+
if let Some(resolve_support) = &code_action.resolve_support {
402+
if resolve_support.properties.iter().any(|it| it == "edit") {
403+
self.client_caps.code_action_resolve = true;
406404
}
407-
_ => (),
408405
}
409406
}
410407
}

crates/rust-analyzer/src/handlers.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,28 @@ pub(crate) fn handle_open_docs(
13221322
Ok(remote.and_then(|remote| Url::parse(&remote).ok()))
13231323
}
13241324

1325+
pub(crate) fn handle_open_cargo_toml(
1326+
snap: GlobalStateSnapshot,
1327+
params: lsp_ext::OpenCargoTomlParams,
1328+
) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
1329+
let _p = profile::span("handle_open_cargo_toml");
1330+
let file_id = from_proto::file_id(&snap, &params.text_document.uri)?;
1331+
let maybe_cargo_spec = CargoTargetSpec::for_file(&snap, file_id)?;
1332+
if maybe_cargo_spec.is_none() {
1333+
return Ok(None);
1334+
}
1335+
1336+
let cargo_spec = maybe_cargo_spec.unwrap();
1337+
let cargo_toml_path = cargo_spec.workspace_root.join("Cargo.toml");
1338+
if !cargo_toml_path.exists() {
1339+
return Ok(None);
1340+
}
1341+
let cargo_toml_url = to_proto::url_from_abs_path(&cargo_toml_path);
1342+
let cargo_toml_location = Location::new(cargo_toml_url, Range::default());
1343+
let res = lsp_types::GotoDefinitionResponse::from(cargo_toml_location);
1344+
Ok(Some(res))
1345+
}
1346+
13251347
fn implementation_title(count: usize) -> String {
13261348
if count == 1 {
13271349
"1 implementation".into()

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,17 @@ impl Request for ExternalDocs {
354354
type Result = Option<lsp_types::Url>;
355355
const METHOD: &'static str = "experimental/externalDocs";
356356
}
357+
358+
pub enum OpenCargoToml {}
359+
360+
impl Request for OpenCargoToml {
361+
type Params = OpenCargoTomlParams;
362+
type Result = Option<lsp_types::GotoDefinitionResponse>;
363+
const METHOD: &'static str = "experimental/openCargoToml";
364+
}
365+
366+
#[derive(Serialize, Deserialize, Debug)]
367+
#[serde(rename_all = "camelCase")]
368+
pub struct OpenCargoTomlParams {
369+
pub text_document: TextDocumentIdentifier,
370+
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ impl GlobalState {
438438
.on::<lsp_ext::CodeActionResolveRequest>(handlers::handle_code_action_resolve)
439439
.on::<lsp_ext::HoverRequest>(handlers::handle_hover)
440440
.on::<lsp_ext::ExternalDocs>(handlers::handle_open_docs)
441+
.on::<lsp_ext::OpenCargoToml>(handlers::handle_open_cargo_toml)
441442
.on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)
442443
.on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)
443444
.on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)

docs/dev/lsp-extensions.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!---
2-
lsp_ext.rs hash: 4f86fb54e4b2870e
2+
lsp_ext.rs hash: 9d5daed5b25dc4f6
33
44
If you need to change the above hash to make the test pass, please check if you
5-
need to adjust this doc as well and ping this issue:
5+
need to adjust this doc as well and ping this issue:
66
77
https://github.com/rust-analyzer/rust-analyzer/issues/4604
88
@@ -537,3 +537,28 @@ Such actions on the client side are appended to a hover bottom as command links:
537537
+-----------------------------+
538538
...
539539
```
540+
541+
## Open Cargo.toml
542+
543+
**Issue:** https://github.com/rust-analyzer/rust-analyzer/issues/6462
544+
545+
This request is sent from client to server to open the current project's Cargo.toml
546+
547+
**Method:** `experimental/openCargoToml`
548+
549+
**Request:** `OpenCargoTomlParams`
550+
551+
**Response:** `Location | null`
552+
553+
554+
### Example
555+
556+
```rust
557+
// Cargo.toml
558+
[package]
559+
// src/main.rs
560+
561+
/* cursor here*/
562+
```
563+
564+
`experimental/openCargoToml` returns a single `Link` to the start of the `[package]` keyword.

editors/code/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@
187187
"command": "rust-analyzer.openDocs",
188188
"title": "Open docs under cursor",
189189
"category": "Rust Analyzer"
190+
},
191+
{
192+
"command": "rust-analyzer.openCargoToml",
193+
"title": "Open Cargo.toml",
194+
"category": "Rust Analyzer"
190195
}
191196
],
192197
"keybindings": [
@@ -1065,6 +1070,10 @@
10651070
{
10661071
"command": "rust-analyzer.openDocs",
10671072
"when": "inRustProject"
1073+
},
1074+
{
1075+
"command": "rust-analyzer.openCargoToml",
1076+
"when": "inRustProject"
10681077
}
10691078
]
10701079
}

editors/code/src/commands.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,27 @@ export function parentModule(ctx: Ctx): Cmd {
188188
};
189189
}
190190

191+
export function openCargoToml(ctx: Ctx): Cmd {
192+
return async () => {
193+
const editor = ctx.activeRustEditor;
194+
const client = ctx.client;
195+
if (!editor || !client) return;
196+
197+
const response = await client.sendRequest(ra.openCargoToml, {
198+
textDocument: ctx.client.code2ProtocolConverter.asTextDocumentIdentifier(editor.document),
199+
});
200+
if (!response) return;
201+
202+
const uri = client.protocol2CodeConverter.asUri(response.uri);
203+
const range = client.protocol2CodeConverter.asRange(response.range);
204+
205+
const doc = await vscode.workspace.openTextDocument(uri);
206+
const e = await vscode.window.showTextDocument(doc);
207+
e.selection = new vscode.Selection(range.start, range.start);
208+
e.revealRange(range, vscode.TextEditorRevealType.InCenter);
209+
};
210+
}
211+
191212
export function ssr(ctx: Ctx): Cmd {
192213
return async () => {
193214
const editor = vscode.window.activeTextEditor;

editors/code/src/lsp_ext.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,9 @@ export interface CommandLinkGroup {
114114
}
115115

116116
export const openDocs = new lc.RequestType<lc.TextDocumentPositionParams, string | void, void>('experimental/externalDocs');
117+
118+
export const openCargoToml = new lc.RequestType<OpenCargoTomlParams, lc.Location, void>("experimental/openCargoToml");
119+
120+
export interface OpenCargoTomlParams {
121+
textDocument: lc.TextDocumentIdentifier;
122+
}

0 commit comments

Comments
 (0)