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

Fix document symbols order #5687

Merged
merged 3 commits into from
Aug 18, 2020
Merged

Fix document symbols order #5687

merged 3 commits into from
Aug 18, 2020

Conversation

magurotuna
Copy link
Contributor

Resolves #5655
And adds tests for handle_document_symbol, both with hierarchical_symbols enabled and with it disabled.

Previously document symbols were displayed in reverse order in sublime text with its LSP plugin, but this patch fixes it like this:

image

@kjeremy
Copy link
Contributor

kjeremy commented Aug 12, 2020

@magurotuna did you file a bug with sublime's LSP?

@@ -273,19 +273,24 @@ pub(crate) fn handle_document_symbol(
parents.push((doc_symbol, symbol.parent));
}
let mut document_symbols = Vec::new();
// Constructs `document_symbols` from `parents`, in order from the end.
while let Some((node, parent)) = parents.pop() {
match parent {
None => document_symbols.push(node),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
None => document_symbols.push(node),
None => {
if let Some(children) = &mut node.children {
children.reverse()
}
document_symbols.push(node)
},

I think should do the trick without recursion.

Copy link
Contributor Author

@magurotuna magurotuna Aug 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion.
I tried your code, but it cannot deal with nested symbols.

For example,

mod a {
    mod b {
        struct B1;
        struct B2;
    }
}

We want this snippet to be output like:

[
  {
    "children": [
      {
        "children": [
          {
            "kind": 23,
            "name": "B1"
          },
          {
            "kind": 23,
            "name": "B2"
          }
        ],
        "kind": 2,
        "name": "b"
      }
    ],
    "kind": 2,
    "name": "a"
  }
]

But actually we get:

[
  {
    "children": [
      {
        "children": [
          {
            "kind": 23,
            "name": "B2"
          },
          {
            "kind": 23,
            "name": "B1"
          }
        ],
        "kind": 2,
        "name": "b"
      }
    ],
    "kind": 2,
    "name": "a"
  }
]

Recursion way works as we expect.

@@ -682,3 +685,313 @@ 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_document_symbol_with_hierarchy() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this test -- heavy tests are very slow, and don't really test much, as the end result is determined by the client anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the end result is determined by the client anyway.

The motivator for this PR is that Sublime displays the results in the order received from the server and doesn't do any sorting on the client side.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's exactly my point: the property we want to check is that "client sensibly interprets the results", and we can't do that without the client.

To put it another way, we could have had the similar test before, with reverse order, and it would not have helped us to find out that the feature is broken in sublime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I have removed the tests. :)

@magurotuna
Copy link
Contributor Author

@kjeremy

did you file a bug with sublime's LSP?

No, I had not made a issue to it. I just made one: sublimelsp/LSP#1253

@matklad
Copy link
Member

matklad commented Aug 18, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Aug 18, 2020

@bors bors bot merged commit 2252a65 into rust-lang:master Aug 18, 2020
@magurotuna magurotuna deleted the fix-document-symbol-order branch August 22, 2020 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document Symbols listed in reverse order within each scope
3 participants