Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
clang: Basic CompleteSourceLocation intent handler
Browse files Browse the repository at this point in the history
  • Loading branch information
quarnster committed Mar 21, 2013
1 parent 99f0115 commit fd65bba
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
6 changes: 4 additions & 2 deletions clang/clang.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ func parseresult(in string) (ret content.CompletionResult, err error) {
return
}

func CompleteAt(loc content.SourceLocation) (ret content.CompletionResult, err error) {
if out, err := RunClang("-fsyntax-only", "-Xclang", fmt.Sprintf("-code-completion-at=%s:%d:%d", loc.File.Name, loc.Line, loc.Column), loc.File.Name); err != nil {
func CompleteAt(args []string, loc content.SourceLocation) (ret content.CompletionResult, err error) {
args = append([]string{"-fsyntax-only", "-Xclang", fmt.Sprintf("-code-completion-at=%s:%d:%d", loc.File.Name, loc.Line, loc.Column)}, args...)
args = append(args, loc.File.Name)
if out, err := RunClang(args...); err != nil {
return ret, err
} else {
return parseresult(string(out))
Expand Down
2 changes: 1 addition & 1 deletion clang/clang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestClang(t *testing.T) {
loc.Column = 1
loc.Line = 10
loc.File.Name = "testdata/hello.cpp"
t.Log(CompleteAt(loc))
t.Log(CompleteAt([]string{}, loc))
}

func TestParseResult(t *testing.T) {
Expand Down
33 changes: 33 additions & 0 deletions clang/complete_intent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package clang

import (
"github.com/quarnster/completion/content"
"regexp"
)

var extre = regexp.MustCompile(`\.(h|hpp|c|cc|cpp|m|mm)$`)

type CompletionHandler struct {
}

func (c *CompletionHandler) Handle(it *content.Intent) *content.Response {
loc := it.Data.Get("loc").(content.SourceLocation)
args, _ := it.Settings().Get("compiler_flags").([]string)
if cmp, err := CompleteAt(args, loc); err != nil {
return content.NewErrorResponse(err.Error())
} else {
r := content.NewResponse()
r.Data.Set("completions", cmp)
return &r
}
}

func (c *CompletionHandler) CanHandle(it *content.Intent) bool {
if it.Operation != content.CompleteSourceLocation {
return false
} else if loc, ok := it.Data.Get("loc").(content.SourceLocation); !ok {
return false
} else {
return extre.MatchString(loc.File.Name)
}
}

0 comments on commit fd65bba

Please sign in to comment.