Skip to content

Commit

Permalink
Merge pull request #10 from lonnywong/master
Browse files Browse the repository at this point in the history
support protobuf keyword completion
  • Loading branch information
lasorda authored Jan 1, 2024
2 parents df4c38c + e0c4eec commit 489b3c5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
24 changes: 21 additions & 3 deletions components/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ import (
"github.com/TobiasYin/go-lsp/lsp/defines"
)

func Completion(ctx context.Context, req *defines.CompletionParams) (result *[]defines.CompletionItem, err error) {
var protoKeywordCompletionItems []defines.CompletionItem

func init() {
kindKeyword := defines.CompletionItemKindKeyword
for _, keyword := range []string{"string", "bytes", "double", "float", "int32", "int64",
"uint32", "uint64", "sint32", "sint64", "fixed32", "fixed64", "sfixed32", "sfixed64", "bool",
"message", "enum", "service", "rpc", "optional", "repeated", "required",
"option", "default", "syntax", "package", "import", "extend", "oneof", "map", "reserved",
} {
insertText := keyword
protoKeywordCompletionItems = append(protoKeywordCompletionItems, defines.CompletionItem{
Kind: &kindKeyword,
Label: keyword,
InsertText: &insertText,
})
}
}

func Completion(ctx context.Context, req *defines.CompletionParams) (*[]defines.CompletionItem, error) {
if !view.IsProtoFile(req.TextDocument.Uri) {
return nil, nil
}
proto_file, err := view.ViewManager.GetFile(req.TextDocument.Uri)
if proto_file.Proto() == nil {
if err != nil || proto_file.Proto() == nil {
return nil, nil
}
line_str := proto_file.ReadLine(int(req.Position.Line))
Expand Down Expand Up @@ -64,7 +82,7 @@ func Completion(ctx context.Context, req *defines.CompletionParams) (result *[]d

func CompletionInThisFile(file view.ProtoFile) (result *[]defines.CompletionItem, err error) {
kindEnum := defines.CompletionItemKindEnum
res := []defines.CompletionItem{}
res := protoKeywordCompletionItems
for _, enums := range file.Proto().Enums() {
res = append(res, defines.CompletionItem{
Label: enums.Protobuf().Name,
Expand Down
4 changes: 3 additions & 1 deletion proto/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ func (v *view) sendDiagnose(document_uri defines.DocumentUri, err error) {
},
}
defer func() {
ViewManager.Server.SendMsg(res)
if len(res.Params.Diagnostics) > 0 {
_ = ViewManager.Server.SendMsg(res)
}
}()
if err == nil {
return
Expand Down

0 comments on commit 489b3c5

Please sign in to comment.