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

Commit

Permalink
Improve status output.
Browse files Browse the repository at this point in the history
+ Small fix in a declarations merging function.
  • Loading branch information
nsf committed Aug 10, 2010
1 parent b64b75f commit 754b030
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
40 changes: 34 additions & 6 deletions decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ const (
DECL_METHODS_STUB
)

var declClassToString = map[int]string{
0: "const",
1: "var",
2: "type",
3: "func",
4: "module",
var declClassToString = [...]string{
DECL_CONST: "const",
DECL_VAR: "var",
DECL_TYPE: "type",
DECL_FUNC: "func",
DECL_MODULE: "module",
DECL_METHODS_STUB: "IF YOU SEE THIS, REPORT A BUG", // :D
}

var declClassToStringDebug = [...]string {
DECL_CONST: " const",
DECL_VAR: " var",
DECL_TYPE: " type",
DECL_FUNC: " func",
DECL_MODULE: "module",
DECL_METHODS_STUB: " stub",
}

type Decl struct {
Expand Down Expand Up @@ -275,6 +285,24 @@ func (d *Decl) Copy(other *Decl) {
d.Embedded = other.Embedded
d.Scope = other.Scope
}
func (other *Decl) DeepCopy() *Decl {
d := new(Decl)
d.Name = other.Name
d.Class = other.Class
d.Type = other.Type
d.Value = other.Value
d.ValueIndex = other.ValueIndex
d.Children = make(map[string]*Decl, len(other.Children))
for key, value := range other.Children {
d.Children[key] = value
}
if other.Embedded != nil {
d.Embedded = make([]ast.Expr, len(other.Embedded))
copy(d.Embedded, other.Embedded)
}
d.Scope = other.Scope
return d
}

func (d *Decl) ClassName() string {
return declClassToString[d.Class]
Expand Down
1 change: 1 addition & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ func (self *PackageFile) processRangeStmt(a *ast.RangeStmt) {
self.topscope = self.scope
if a.Tok == token.DEFINE {
var t1, t2 ast.Expr
// TODO: deal with typedefed slices/maps here
t1 = NewDeclVar("tmp", nil, a.X, -1, self.scope).InferType(self.ctx)
if t1 != nil {
// figure out range Key, Value types
Expand Down
64 changes: 64 additions & 0 deletions gocodelib.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,40 @@ func (self *AutoCompleteContext) Apropos(file []byte, filename string, cursor in
return b.names, b.types, b.classes, partial
}

//-------------------------------------------------------------------------
// Status output
//-------------------------------------------------------------------------

type DeclSlice []*Decl
func (s DeclSlice) Less(i, j int) bool {
if declClassToString[s[i].Class][0] == declClassToString[s[j].Class][0] {
return s[i].Name < s[j].Name
}
return declClassToString[s[i].Class] < declClassToString[s[j].Class]
}
func (s DeclSlice) Len() int { return len(s) }
func (s DeclSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

const (
COLOR_red = "\033[0;31m"
COLOR_RED = "\033[1;31m"
COLOR_green = "\033[0;32m"
COLOR_GREEN = "\033[1;32m"
COLOR_yellow = "\033[0;33m"
COLOR_YELLOW = "\033[1;33m"
COLOR_blue = "\033[0;34m"
COLOR_BLUE = "\033[1;34m"
COLOR_magenta = "\033[0;35m"
COLOR_MAGENTA = "\033[1;35m"
COLOR_cyan = "\033[0;36m"
COLOR_CYAN = "\033[1;36m"
COLOR_white = "\033[0;37m"
COLOR_WHITE = "\033[1;37m"
NC = "\033[0m"
)

const STATUS_DECLS = "\t(class: %s, nchildren: %2d)\t"+ COLOR_yellow +"%s"+ NC +"\n"

func (self *AutoCompleteContext) Status() string {
buf := bytes.NewBuffer(make([]byte, 0, 4096))
fmt.Fprintf(buf, "Server's GOMAXPROCS == %d\n", runtime.GOMAXPROCS(0))
Expand Down Expand Up @@ -475,6 +509,36 @@ func (self *AutoCompleteContext) Status() string {
for _, f := range self.others {
fmt.Fprintf(buf, "\t%s\n", f.name)
}
fmt.Fprintf(buf, "\nListing declarations from files:\n")

var ds DeclSlice
var i int

fmt.Fprintf(buf, "\n%s:\n", self.current.name)
ds = make(DeclSlice, len(self.current.decls))
i = 0
for _, d := range self.current.decls {
ds[i] = d
i++
}
sort.Sort(ds)
for _, d := range ds {
fmt.Fprintf(buf, STATUS_DECLS, declClassToStringDebug[d.Class], len(d.Children), d.Name)
}

for _, f := range self.others {
fmt.Fprintf(buf, "\n%s:\n", f.name)
ds = make(DeclSlice, len(f.decls))
i = 0
for _, d := range f.decls {
ds[i] = d
i++
}
sort.Sort(ds)
for _, d := range ds {
fmt.Fprintf(buf, STATUS_DECLS, declClassToStringDebug[d.Class], len(d.Children), d.Name)
}
}
}
return buf.String()
}
2 changes: 2 additions & 0 deletions scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func (s *Scope) mergeDecl(d *Decl) {
if !ok {
s.entities[d.Name] = d
} else {
decl := decl.DeepCopy()
decl.ExpandOrReplace(d)
s.entities[d.Name] = decl
}
}

Expand Down

0 comments on commit 754b030

Please sign in to comment.