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

Show documentation on hover and completion #101

Merged
merged 8 commits into from
Jan 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/internal/lsp/cst/parser.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,9 @@ package cst
//TSLanguage *tree_sitter_c3();
import "C"
import (
sitter "github.com/smacker/go-tree-sitter"
"unsafe"

sitter "github.com/smacker/go-tree-sitter"
)

func NewSitterParser() *sitter.Parser {
31 changes: 29 additions & 2 deletions server/internal/lsp/search/search_completion_list.go
Original file line number Diff line number Diff line change
@@ -127,6 +127,21 @@ func extractExplicitModulePath(possibleModulePath string) option.Option[symbols.
return option.None[symbols.ModulePath]()
}

// Obtains a doc comment's representation as markup, or nil.
// Only the body is included (not contracts) for brevity.
// Returns: nil | MarkupContent
func GetCompletableDocComment(s symbols.Indexable) any {
docComment := s.GetDocComment()
if docComment == nil || docComment.GetBody() == "" {
return nil
} else {
return protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: docComment.GetBody(),
}
}
}

// Returns: []CompletionItem | CompletionList | nil
func (s *Search) BuildCompletionList(
ctx context.CursorContext,
@@ -222,6 +237,9 @@ func (s *Search) BuildCompletionList(
items = append(items, protocol.CompletionItem{
Label: member.GetName(),
Kind: &member.Kind,

// At this moment, struct members cannot receive documentation
Documentation: nil,
Copy link
Owner

Choose a reason for hiding this comment

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

Is this because of how you decide which doc comment you assign to a symbol? (Las doc block parsed belongs to next identified symbol)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In part, yes: I'd have to replicate my code to detect and assign doc comments in the struct parsing function as well in order to accept doc comments inside the struct, i.e. to accept something like

struct Name {
  <* doc *>
  int a;
}

But also, I didn't do that because it doesn't compile (through c3c), even though the tree-sitter grammar accepts it.

We could, of course, just copy and paste the struct's own docs to all of its members, but that doesn't feel ideal, at least not at the moment.

})
}
}
@@ -252,6 +270,7 @@ func (s *Search) BuildCompletionList(
NewText: fn.GetMethodName(),
Range: replacementRange,
},
Documentation: GetCompletableDocComment(fn),
})
}

@@ -262,6 +281,9 @@ func (s *Search) BuildCompletionList(
items = append(items, protocol.CompletionItem{
Label: enumerator.GetName(),
Kind: &enumerator.Kind,

// No documentation for enumerators at this time
Documentation: nil,
})
}
}
@@ -273,6 +295,9 @@ func (s *Search) BuildCompletionList(
items = append(items, protocol.CompletionItem{
Label: constant.GetName(),
Kind: &constant.Kind,

// No documentation for fault constants at this time
Documentation: nil,
})
}
}
@@ -310,11 +335,13 @@ func (s *Search) BuildCompletionList(
NewText: storedIdentifier.GetName(),
Range: editRange,
},
Documentation: GetCompletableDocComment(storedIdentifier),
})
} else {
items = append(items, protocol.CompletionItem{
Label: storedIdentifier.GetName(),
Kind: cast.ToPtr(storedIdentifier.GetKind()),
Label: storedIdentifier.GetName(),
Kind: cast.ToPtr(storedIdentifier.GetKind()),
Documentation: GetCompletableDocComment(storedIdentifier),
})
}
}
Loading