Skip to content

Commit 005a351

Browse files
authored
Merge pull request #3 from x1unix/dev
Improve code completion
2 parents 1c085e6 + a10c7d3 commit 005a351

File tree

4 files changed

+100
-21
lines changed

4 files changed

+100
-21
lines changed

pkg/analyzer/decl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func funcToItem(fn *ast.FuncDecl) *CompletionItem {
8181
ci := &CompletionItem{
8282
Label: fn.Name.String(),
8383
Kind: Function,
84-
Documentation: fn.Doc.Text(),
84+
Documentation: formatDoc(fn.Doc.Text()),
8585
}
8686

8787
ci.Detail = funcToString(fn.Type)

pkg/analyzer/docfmt.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package analyzer
2+
3+
import "strings"
4+
5+
const (
6+
newLineChar = '\n'
7+
tabChar = '\t'
8+
)
9+
10+
var (
11+
mdCodeTag = []byte("```\n")
12+
spaceIdent = " "
13+
spaceIdentLen = len(spaceIdent)
14+
)
15+
16+
func isDocLine(line string) bool {
17+
// Source code in Go usually doc starts with tab char
18+
if line[0] == tabChar {
19+
return true
20+
}
21+
22+
// Workaround for some packages with double space as doc indent (line "net/http")
23+
if (len(line) > spaceIdentLen) && (line[:spaceIdentLen] == spaceIdent) {
24+
return true
25+
}
26+
27+
return false
28+
}
29+
30+
func formatDoc(str string) MarkdownString {
31+
if str == "" {
32+
return MarkdownString{Value: str}
33+
}
34+
35+
w := strings.Builder{}
36+
docStart := false
37+
38+
lines := strings.Split(str, "\n")
39+
for _, line := range lines {
40+
if len(line) == 0 {
41+
w.WriteRune(newLineChar)
42+
continue
43+
}
44+
45+
// Source code in Go doc starts with tab char
46+
if isDocLine(line) {
47+
if !docStart {
48+
// Put markdown code section
49+
// if we met first source code line
50+
docStart = true
51+
w.Write(mdCodeTag)
52+
}
53+
54+
w.WriteString(line)
55+
w.WriteRune(newLineChar)
56+
continue
57+
}
58+
59+
// Else - regular text
60+
if docStart {
61+
// Terminate code block if previous
62+
// was open and not terminated
63+
docStart = false
64+
w.Write(mdCodeTag)
65+
}
66+
67+
w.WriteString(line)
68+
w.WriteRune(newLineChar)
69+
}
70+
71+
if docStart {
72+
// close markdown code block if wasn't closed
73+
w.Write(mdCodeTag)
74+
}
75+
76+
return MarkdownString{
77+
Value: w.String(),
78+
}
79+
}

pkg/analyzer/scanner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (p *PackageScanner) appendFunc(fn *ast.FuncDecl, dest *SymbolIndex) {
7979
func (p *PackageScanner) Scan() (PackageSummary, error) {
8080
sum := NewPackageSummary()
8181
set := token.NewFileSet()
82-
packs, err := parser.ParseDir(set, p.path, nil, 0)
82+
packs, err := parser.ParseDir(set, p.path, nil, parser.ParseComments)
8383
if err != nil {
8484
return sum, err
8585
}

web/src/editor/provider.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ const COMPL_REGEXP = /([a-zA-Z0-9_]+)(\.([A-Za-z0-9_]+))?$/;
1515
const R_GROUP_PKG = 1;
1616
const R_GROUP_METHOD = 3;
1717

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

21-
private parseExpression(expr: string) {
22-
COMPL_REGEXP.lastIndex = 0; // Reset regex state
23-
const m = COMPL_REGEXP.exec(expr);
24-
if (!m) {
25-
return null;
26-
}
25+
const varName = m[R_GROUP_PKG];
26+
const propValue = m[R_GROUP_METHOD];
2727

28-
const varName = m[R_GROUP_PKG];
29-
const propValue = m[R_GROUP_METHOD];
28+
if (!propValue) {
29+
return {value: varName};
30+
}
3031

31-
if (!propValue) {
32-
return {value: varName};
33-
}
32+
return {
33+
packageName: varName,
34+
value: propValue
35+
};
36+
};
3437

35-
return {
36-
packageName: varName,
37-
value: propValue
38-
};
39-
}
38+
class GoCompletionItemProvider implements monaco.languages.CompletionItemProvider {
39+
constructor(private client: IAPIClient) {}
4040

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

49-
const query = this.parseExpression(val);
49+
const query = parseExpression(val);
5050
if (!query) {
5151
return Promise.resolve({suggestions: []});
5252
}

0 commit comments

Comments
 (0)