Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/analyzer/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func funcToItem(fn *ast.FuncDecl) *CompletionItem {
ci := &CompletionItem{
Label: fn.Name.String(),
Kind: Function,
Documentation: fn.Doc.Text(),
Documentation: formatDoc(fn.Doc.Text()),
}

ci.Detail = funcToString(fn.Type)
Expand Down
79 changes: 79 additions & 0 deletions pkg/analyzer/docfmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package analyzer

import "strings"

const (
newLineChar = '\n'
tabChar = '\t'
)

var (
mdCodeTag = []byte("```\n")
spaceIdent = " "
spaceIdentLen = len(spaceIdent)
)

func isDocLine(line string) bool {
// Source code in Go usually doc starts with tab char
if line[0] == tabChar {
return true
}

// Workaround for some packages with double space as doc indent (line "net/http")
if (len(line) > spaceIdentLen) && (line[:spaceIdentLen] == spaceIdent) {
return true
}

return false
}

func formatDoc(str string) MarkdownString {
if str == "" {
return MarkdownString{Value: str}
}

w := strings.Builder{}
docStart := false

lines := strings.Split(str, "\n")
for _, line := range lines {
if len(line) == 0 {
w.WriteRune(newLineChar)
continue
}

// Source code in Go doc starts with tab char
if isDocLine(line) {
if !docStart {
// Put markdown code section
// if we met first source code line
docStart = true
w.Write(mdCodeTag)
}

w.WriteString(line)
w.WriteRune(newLineChar)
continue
}

// Else - regular text
if docStart {
// Terminate code block if previous
// was open and not terminated
docStart = false
w.Write(mdCodeTag)
}

w.WriteString(line)
w.WriteRune(newLineChar)
}

if docStart {
// close markdown code block if wasn't closed
w.Write(mdCodeTag)
}

return MarkdownString{
Value: w.String(),
}
}
2 changes: 1 addition & 1 deletion pkg/analyzer/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (p *PackageScanner) appendFunc(fn *ast.FuncDecl, dest *SymbolIndex) {
func (p *PackageScanner) Scan() (PackageSummary, error) {
sum := NewPackageSummary()
set := token.NewFileSet()
packs, err := parser.ParseDir(set, p.path, nil, 0)
packs, err := parser.ParseDir(set, p.path, nil, parser.ParseComments)
if err != nil {
return sum, err
}
Expand Down
38 changes: 19 additions & 19 deletions web/src/editor/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ const COMPL_REGEXP = /([a-zA-Z0-9_]+)(\.([A-Za-z0-9_]+))?$/;
const R_GROUP_PKG = 1;
const R_GROUP_METHOD = 3;

class GoCompletionItemProvider implements monaco.languages.CompletionItemProvider {
constructor(private client: IAPIClient) {}
const parseExpression = (expr: string) => {
COMPL_REGEXP.lastIndex = 0; // Reset regex state
const m = COMPL_REGEXP.exec(expr);
if (!m) {
return null;
}

private parseExpression(expr: string) {
COMPL_REGEXP.lastIndex = 0; // Reset regex state
const m = COMPL_REGEXP.exec(expr);
if (!m) {
return null;
}
const varName = m[R_GROUP_PKG];
const propValue = m[R_GROUP_METHOD];

const varName = m[R_GROUP_PKG];
const propValue = m[R_GROUP_METHOD];
if (!propValue) {
return {value: varName};
}

if (!propValue) {
return {value: varName};
}
return {
packageName: varName,
value: propValue
};
};

return {
packageName: varName,
value: propValue
};
}
class GoCompletionItemProvider implements monaco.languages.CompletionItemProvider {
constructor(private client: IAPIClient) {}

async provideCompletionItems(model: ITextModel, position: Position, context: CompletionContext, token: CancellationToken): Promise<CompletionList> {
const val = model.getValueInRange({
Expand All @@ -46,7 +46,7 @@ class GoCompletionItemProvider implements monaco.languages.CompletionItemProvide
endColumn: position.column,
}).trim();

const query = this.parseExpression(val);
const query = parseExpression(val);
if (!query) {
return Promise.resolve({suggestions: []});
}
Expand Down