Exemplo n.º 1
0
func (d *DWARFHelper) Load() (content.CompletionResult, error) {
	var cmp content.CompletionResult
	r := d.df.Reader()
	for {
		if e, err := r.Next(); err != nil {
			return cmp, err
			break
		} else if e == nil {
			break
		} else {
			switch e.Tag {
			case dwarf.TagMember:
				if f, err := d.toContentField(e); err != nil {
					return cmp, err
				} else {
					cmp.Fields = append(cmp.Fields, f)
				}
			case dwarf.TagSubprogram:
				if m, err := d.toContentMethod(e); err != nil {
					return cmp, err
				} else {
					cmp.Methods = append(cmp.Methods, m)
				}
			case dwarf.TagClassType, dwarf.TagTypedef, dwarf.TagStructType:
				if t, err := d.GetType(e.Offset); err != nil {
					return cmp, err
				} else {
					cmp.Types = append(cmp.Types, t)
				}
			}
		}
	}
	return cmp, nil
}
Exemplo n.º 2
0
func (g *Go) complete_pkg(pkg string, cmp *content.CompletionResult) error {
	if g.imports == nil {
		g.imports = make(map[string]*types.Package)
	}

	if p, err := types.GcImport(g.imports, pkg); err != nil {
		return err
	} else {
		nn := p.Scope()
		for i := 0; i < nn.NumEntries(); i++ {
			t := nn.At(i)

			var flags content.Flags

			if n := t.Name(); n[0] != strings.ToUpper(n)[0] {
				flags = content.FLAG_ACC_PROTECTED
			} else {
				flags = content.FLAG_ACC_PUBLIC
			}
			switch t.(type) {
			case *types.Func:
				var m content.Method
				m.Flags |= flags
				m.Name.Relative = t.Name()
				sig := t.Type().Underlying().(*types.Signature)
				if sig.Recv() != nil {
					continue
				}
				par := sig.Params()
				for j := 0; j < par.Len(); j++ {
					m.Parameters = append(m.Parameters, g.pkg_var(par.At(j)))
				}
				ret := sig.Results()
				for j := 0; j < ret.Len(); j++ {
					m.Returns = append(m.Returns, g.pkg_var(ret.At(j)))
				}
				cmp.Methods = append(cmp.Methods, m)
			case *types.TypeName:
				var t2 content.Type
				t2.Flags |= flags
				t2.Name.Relative = t.Name()
				switch t.Type().Underlying().(type) {
				case *types.Interface:
					t2.Flags |= content.FLAG_TYPE_INTERFACE
				case *types.Struct:
					t2.Flags |= content.FLAG_TYPE_STRUCT
				}
				cmp.Types = append(cmp.Types, t2)
			case *types.Const, *types.Var:
				var f content.Field
				f.Name.Relative = t.Name()
				f.Type = g.pkg_type(t.Type())
				cmp.Fields = append(cmp.Fields, f)
			default:
				log4go.Warn("Unimplemented type in package completion: at: %+v, %v, %v", t, reflect.TypeOf(t), reflect.TypeOf(t.Type().Underlying()))
			}
		}
	}
	return nil
}
Exemplo n.º 3
0
func (c *Java) Complete(args *content.CompleteArgs, cmp *content.CompletionResult) error {
	log4go.Fine("%+v", args)
	var archive Archive
	session := args.Session()
	if session != nil {
		if a, ok := session.Get("java_archive").(Archive); ok {
			archive = a
		}
	}
	if archive == nil {
		cp, err := DefaultClasspath()
		if err != nil {
			// We don't really care about not being able to get the default classpath as it could be provided manually by the user
			log4go.Warn("Couldn't get the default classpath: %s", err)
		}
		settings := args.Settings()
		if cp2, ok := settings.Get("java_classpath").([]string); ok {
			// TODO: do we ever want to override rather than append to the classpath?
			cp = append(cp, cp2...)
		}
		log4go.Fine("classpath: %+v", cp)
		if archive, err = NewCompositeArchive(cp); err != nil {
			return err
		} else if session != nil {
			session.Set("java_archive", archive)
		}
	}

	className, err := fqnToClassname(args.Location)
	if err != nil {
		return err
	}

	data, err := archive.LoadClass(className)
	if err != nil {
		return err
	}

	class, err := NewClass(bytes.NewReader(data))
	if err != nil {
		return err
	}

	ct, err := class.ToContentType()
	if err != nil {
		return err
	}

	// TODO(q): Inherited fields and methods?
	// 			I see value in being able to "just" get the smaller set,
	//			but getting the full set should definitely be possible "server side"
	cmp.Fields = ct.Fields
	cmp.Types = ct.Types
	cmp.Methods = ct.Methods

	return nil
}
Exemplo n.º 4
0
func (d *DWARFHelper) Complete(id content.FullyQualifiedName) (content.CompletionResult, error) {
	var cmp content.CompletionResult
	r := d.df.Reader()
	for {
		e, err := r.Next()
		if err != nil {
			return cmp, err
		} else if e == nil {
			break
		}
		switch e.Tag {
		case dwarf.TagCompileUnit:
			continue
		case dwarf.TagClassType, dwarf.TagTypedef, dwarf.TagStructType:
			t, err := d.GetType(e.Offset)
			if err != nil {
				return cmp, err
			}
			if t.Name != id || !e.Children {
				r.SkipChildren()
				continue
			}
			r2 := d.df.Reader()
			r2.Seek(e.Offset)
			for {
				e, err := r.Next()
				if err != nil {
					return cmp, err
				} else if e == nil || e.Tag == 0 {
					break
				}
				switch e.Tag {
				case dwarf.TagMember:
					if f, err := d.toContentField(e); err != nil {
						return cmp, err
					} else {
						f.Name.Absolute = fmt.Sprintf("dwarf://field/%s;%d", d.id, e.Offset)
						cmp.Fields = append(cmp.Fields, f)
					}
				case dwarf.TagSubprogram:
					if m, err := d.toContentMethod(e); err != nil {
						return cmp, err
					} else {
						m.Name.Absolute = fmt.Sprintf("dwarf://method/%s;%d", d.id, e.Offset)
						cmp.Methods = append(cmp.Methods, m)
					}
					r.SkipChildren()
				}
			}
			return cmp, nil
		default:
			r.SkipChildren()
		}
	}
	return cmp, errors.New(fmt.Sprintf("Unable to find type: %s", id))
}
Exemplo n.º 5
0
func (c *Net) complete(cache *Cache, t *content.Type, cmp *content.CompletionResult) error {
	if ct, err := cache.Complete(t); err != nil {
		return err
	} else {
		cmp.Fields = ct.Fields
		cmp.Types = ct.Types
		cmp.Methods = ct.Methods
	}
	return nil
}
Exemplo n.º 6
0
func (g *Go) complete(t reflect.Type, cmp *content.CompletionResult) error {
	if k := t.Kind(); k != reflect.Ptr {
		return fmt.Errorf("Expected a pointer to a type not %v", k)
	}
	for i := 0; i < t.NumMethod(); i++ {
		m := t.Method(i)
		cmp.Methods = append(cmp.Methods, g.method(m))
	}

	t = t.Elem()
	for i := 0; i < t.NumMethod(); i++ {
		m := t.Method(i)
		cmp.Methods = append(cmp.Methods, g.method(m))
	}

	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)
		cmp.Fields = append(cmp.Fields, g.field(f))
	}

	return nil
}
Exemplo n.º 7
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
}