예제 #1
0
func (c *Clang) CompleteAt(a *content.CompleteAtArgs, ret *content.CompletionResult) error {
	origargs, _ := a.Settings().Get("compiler_flags").([]string)
	args := make([]string, len(origargs))
	for i := range origargs {
		args[i] = expand_path.ExpandPath(origargs[i])
	}
	fn := a.Location.File.Name

	if cnt := a.Location.File.Contents; cnt != "" {
		if f, err := ioutil.TempFile("", "completion_clang"); err != nil {
			return err
		} else {
			fn = f.Name()
			defer os.Remove(fn)
			fn += filepath.Ext(a.Location.File.Name)
			defer os.Remove(fn)
			if err := ioutil.WriteFile(fn, []byte(cnt), 0644); err != nil {
				return err
			}
		}
		args = append(args, "-I"+filepath.Dir(a.Location.File.Name))
	}

	args = append([]string{"-fsyntax-only", "-Xclang", fmt.Sprintf("-code-completion-at=%s:%d:%d", fn, a.Location.Line, a.Location.Column)}, args...)
	args = append(args, fn)
	if out, err := RunClang(args...); len(out) == 0 {
		return err
	} else if r, err := parseresult(string(out)); err != nil {
		return err
	} else {
		*ret = r
		return nil
	}
}
예제 #2
0
func TestNet(t *testing.T) {
	paths := DefaultPaths()
	if len(paths) == 0 {
		t.Skip("Neither mono nor Windows .NET Framework paths were possible to get")
	}

	var (
		n    Net
		args content.CompleteAtArgs
		cmp  content.CompletionResult
	)

	tests := []struct {
		InFile       string
		Line, Column uint
	}{
		// TODO: this test is not platform independent as it depends on whatever framework you happen
		//       to have installed
		{"./testdata/CompleteSharp.cs", 40, 27},
		{"./testdata/CompleteSharp.cs", 40, 41},
		{"./testdata/CompleteSharp.cs", 47, 47},
		//{"./testdata/CompleteSharp.cs", 28, 14},
		{"./testdata/CompleteSharp.cs", 211, 46},
		{"./testdata/CompleteSharp.cs", 761, 83},
		{"./testdata/CompleteSharp.cs", 761, 27},
		{"./testdata/CompleteSharp.cs", 857, 29},
		{"./testdata/CompleteSharp.cs", 737, 15},
		{"./testdata/CompleteSharp.cs", 95, 38},
		{"./testdata/CompleteSharp.cs", 95, 45},
		{"./testdata/CompleteSharp.cs", 776, 39},
		{"./testdata/NamespaceTest.cs", 4, 15},
		{"./testdata/NamespaceTest.cs", 6, 15},
	}
	args.SessionId = "a"
	args.Settings().Set("net_paths", []string{"./testdata/"})
	args.Settings().Set("net_assemblies", []string{"CompleteSharp.exe"})
	for _, test := range tests {
		args.Location.File.Name = test.InFile
		args.Location.Line = test.Line
		args.Location.Column = test.Column
		ex := fmt.Sprintf("%s-%d-%d.cmp", test.InFile, test.Line, test.Column)
		if err := n.CompleteAt(&args, &cmp); err != nil {
			t.Errorf("Unable to complete %v: %s", test, err)
		} else if exp, err := ioutil.ReadFile(ex); err != nil {
			t.Logf("Couldn't read the expected output file %s (%s); it'll be created", ex, err)
			if err := ioutil.WriteFile(ex, []byte(cmp.String()), 0644); err != nil {
				t.Error(err)
			}
		} else if d := util.Diff(string(exp), cmp.String()); len(d) != 0 {
			t.Error(d)
		}
	}
}
예제 #3
0
func (c *Clang) prepare(a *content.CompleteAtArgs) (fn string, args []string, err error) {
	origargs, _ := a.Settings().Get("compiler_flags").([]string)
	args = make([]string, len(origargs))
	for i := range origargs {
		args[i] = expand_path.ExpandPath(origargs[i])
	}
	fn = a.Location.File.Name
	if a.Location.File.Contents != "" {
		// File is unsaved, so use stdin as the filename
		fn = "-"
	}

	return fn, args, nil
}
예제 #4
0
func (t *Clang) CompleteAt(args *content.CompleteAtArgs, res *content.CompletionResult) error {
	origargs, _ := args.Settings().Get("compiler_flags").([]string)
	var unsaved map[string]string
	if args.Location.File.Contents != "" {
		unsaved = map[string]string{args.Location.File.Name: args.Location.File.Contents}
	}

	if tu := t.GetTranslationUnit(args.Location.File.Name, origargs, "", unsaved); tu == nil {
		return nil
	}
	cres := tu.CompleteAt(args.Location.File.Name, int(args.Location.Line), int(args.Location.Column), unsaved, 0)
	if !cres.IsValid() {
		return fmt.Errorf("CompleteResults is not valid")
	}
	defer cres.Dispose()
	for _, r := range cres.Results() {
		var (
			buf bytes.Buffer
		)
		switch r.CursorKind {
		case clang.CK_StructDecl, clang.CK_TypedefDecl:
			for _, c := range r.CompletionString.Chunks() {
				buf.WriteString(c.Text())
			}
			var tt content.Type
			tt.Flags = content.FLAG_TYPE_CLASS
			tt.Name.Absolute = buf.String()
			res.Types = append(res.Types, tt)
		case clang.CK_FunctionDecl:
			var (
				m            content.Method
				paramstarted bool
				argCount     int
			)
			for _, c := range r.CompletionString.Chunks() {
				switch k := c.Kind(); k {
				case clang.CompletionChunk_ResultType:
					var v content.Variable
					v.Name.Relative = c.Text()
					m.Returns = append(m.Returns, v)
				case clang.CompletionChunk_Placeholder:
					var v content.Variable
					v.Type.Name.Relative = c.Text()
					v.Name.Relative = fmt.Sprintf("arg%d", argCount)
					argCount++
					m.Parameters = append(m.Parameters, v)
				case clang.CompletionChunk_LeftParen:
					paramstarted = true
				case clang.CompletionChunk_RightParen, clang.CompletionChunk_Comma:
				case clang.CompletionChunk_TypedText:
					if !paramstarted {
						buf.WriteString(c.Text())
					}
				default:
					log4go.Warn("Unimplemented CompletionChunkKind: %s", k)
				}
			}
			m.Name.Relative = buf.String()
			res.Methods = append(res.Methods, m)
		default:
			log4go.Warn("Unimplemented CursorKind: %s", r.CursorKind)
		}
	}
	return nil
}