Skip to content

Don’t include local variables in document symbols #973

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

Merged

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Nov 23, 2023

Fixes #962
rdar://117810784


While at it, also include // MARK: comments in the document symbols request.

Fixes #963
rdar://117811210

@ahoppen
Copy link
Member Author

ahoppen commented Nov 23, 2023

@swift-ci Please test

@ahoppen ahoppen force-pushed the ahoppen/no-local-vars-in-document-symbols branch 2 times, most recently from e90d614 to 46328c9 Compare November 27, 2023 20:28
@ahoppen
Copy link
Member Author

ahoppen commented Nov 27, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Nov 27, 2023

@swift-ci Please test Windows

switch piece {
case .lineComment(let commentText), .blockComment(let commentText):
let trimmedComment = commentText.trimmingPrefix(while: { $0 == "/" || $0 == "*" || $0 == " " })
if trimmedComment.starts(with: "MARK: ") {
Copy link
Contributor

Choose a reason for hiding this comment

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

One day we'll get to use nice things (trimmingPrefix). How about:

fileprivate let MARK_PREFIX = "MARK: "
...
if trimmedComment.starts(with: MARK_PREFIX) {
  let markText = trimmedComment.dropFirst(MARK_PREFIX.count)
...

?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea 👍🏽

switch piece {
case .lineComment:
documentSymbolName = markText.trimmingCharacters(in: CharacterSet(["/", "*"]).union(.whitespaces))
case .blockComment: documentSymbolName = markText.trimmingCharacters(in: .whitespaces)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could have something like:

/****************
 * MARK: foobar *
 ****************/

😅

Any reason in particular not to just always trim / and* (which you could then move up into trimmedComment)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Did that

Comment on lines 282 to 283
if self.parent?.is(MemberBlockItemSyntax.self) ?? false {
return true
} else if let codeBlockItem = self.parent?.as(CodeBlockItemSyntax.self),
codeBlockItem.parent?.parent?.is(SourceFileSyntax.self) ?? false
{
return true
} else {
return false
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you really find that easier to read than eg.

    if self.parent?.is(MemberBlockItemSyntax.self) ?? false {
      return true
    }

    if let codeBlockItem = self.parent?.as(CodeBlockItemSyntax.self),
      codeBlockItem.parent?.parent?.is(SourceFileSyntax.self) ?? false {
      return true
    }

    return false

Copy link
Member Author

Choose a reason for hiding this comment

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

I don’t have super strong opinions. Changed it

func trimmingPrefix(while condition: (Character) -> Bool) -> Substring {
var result = self[...]
while let first = result.first, condition(first) {
result = result[result.index(after: result.startIndex)...]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
result = result[result.index(after: result.startIndex)...]
result = result.dropFirst()

Copy link
Contributor

Choose a reason for hiding this comment

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

Also given my comment above, you could add the other version of trimmingPrefix that takes a Sequence

Copy link
Member Author

Choose a reason for hiding this comment

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

I just removed it and am now trimming /, * and whitespace on either side of the comment.

@ahoppen ahoppen force-pushed the ahoppen/no-local-vars-in-document-symbols branch from 46328c9 to fe919c4 Compare November 27, 2023 23:40
@ahoppen ahoppen force-pushed the ahoppen/no-local-vars-in-document-symbols branch from fe919c4 to b85f3af Compare November 27, 2023 23:56
@ahoppen
Copy link
Member Author

ahoppen commented Nov 27, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Nov 27, 2023

@swift-ci Please test Windows

@ahoppen
Copy link
Member Author

ahoppen commented Nov 28, 2023

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 2f6a3d9 into swiftlang:main Nov 28, 2023
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.

textDocument/documentSymbol should return entries for MARK comments textDocument/documentSymbol should not return local variables
3 participants