Skip to content

Commit 710caf0

Browse files
committed
chore: add loop directives now showup only in loops
1 parent 9083485 commit 710caf0

3 files changed

Lines changed: 59 additions & 14 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
log.txt
22
main
3+
Session.vim

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# Release Notes
22

3+
## v0.4.0 (2025-11-07)
4+
- ✨ Loop directives like `@break`, `@continue`, `@breakIf`, `@continueIf` are now getting suggested only inside of loops.
5+
36
## v0.3.1 (2025-06-14)
4-
- 🐛 Update LSP textwire to the latest version which will fix `@break($1)` snippet to `@break`
7+
- 🐛 Updated LSP textwire to the latest version which will fix `@break($1)` snippet to `@break`
58

69
## v0.3.0 (2025-06-14)
7-
- 🐛 Update LSP textwire to the latest version which will fix crashing LSP when you make a syntax error in your Textwire code
10+
- 🐛 Updated LSP textwire to the latest version which will fix crashing LSP when you make a syntax error in your Textwire code
811
- ✨ Autocomplete snippets that appear after you hit enter are now more complex. Instead of simple autocomple like `@if` you know get the full if statement and the cursor placed inside condition. It allows you to hit tab to move to the next place in a snippet
912

1013
## v0.2.0 (2025-05-30)
11-
-Add autocompletion for loop object. Now if you type `loop.` inside of a loop, it will show available properties on the object
14+
-Added autocompletion for loop object. Now if you type `loop.` inside of a loop, it will show available properties on the object
1215

1316
## v0.1.4 (2025-05-17)
1417
- ✨ Autocomplete suggestions show code example with syntax highlighting. Before, it was just displayed as text

analysis/completion.go

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package analysis
22

33
import (
44
"regexp"
5+
"slices"
56
"strings"
67

78
"github.com/textwire/lsp/internal/logger"
@@ -35,18 +36,9 @@ func (s *State) Completion(id int, uri string, pos lsp.Position) (lsp.Completion
3536
var err error
3637

3738
if directiveMatch != nil {
38-
completionItems, err = completions.GetDirectives(locale)
39+
completionItems, err = handleDirectivesAutocomplete(doc, pos, uri)
3940
} else if strings.HasSuffix(textBeforeCursor, "loop.") {
40-
doc = removeTrailingChar(doc, pos.Line, pos.Character, '.')
41-
42-
isInsideLoop, errors := twLsp.IsInLoop(doc, uri, pos.Line, pos.Character)
43-
if len(errors) > 0 {
44-
logger.Error.Println(errors[0])
45-
}
46-
47-
if isInsideLoop {
48-
completionItems, err = completions.GetLoopObjFields(locale)
49-
}
41+
completionItems, err = handleLoopObjectAutocomplete(doc, pos, uri)
5042
}
5143

5244
if err != nil {
@@ -57,6 +49,55 @@ func (s *State) Completion(id int, uri string, pos lsp.Position) (lsp.Completion
5749
return s.completionResponse(id, s.makeCompletions(completionItems)), nil
5850
}
5951

52+
func handleDirectivesAutocomplete(doc string, pos lsp.Position, uri string) ([]completions.Completion, error) {
53+
dirs, err := completions.GetDirectives(locale)
54+
if err != nil {
55+
return []completions.Completion{}, err
56+
}
57+
58+
isInsideLoop, errors := twLsp.IsInLoop(doc, uri, pos.Line, pos.Character)
59+
if len(errors) > 0 {
60+
logger.Error.Println(errors[0])
61+
}
62+
63+
if isInsideLoop {
64+
return dirs, nil
65+
}
66+
67+
filteredDirs := make([]completions.Completion, 0, len(dirs))
68+
loopDirs := []string{"@break", "@breakIf", "@continue", "@continueIf"}
69+
70+
for _, dir := range dirs {
71+
if slices.Contains(loopDirs, dir.Label) {
72+
continue
73+
}
74+
75+
filteredDirs = append(filteredDirs, dir)
76+
}
77+
78+
return filteredDirs, nil
79+
}
80+
81+
func handleLoopObjectAutocomplete(doc string, pos lsp.Position, uri string) ([]completions.Completion, error) {
82+
doc = removeTrailingChar(doc, pos.Line, pos.Character, '.')
83+
84+
isInsideLoop, errors := twLsp.IsInLoop(doc, uri, pos.Line, pos.Character)
85+
if len(errors) > 0 {
86+
logger.Error.Println(errors[0])
87+
}
88+
89+
if !isInsideLoop {
90+
return []completions.Completion{}, nil
91+
}
92+
93+
fields, err := completions.GetLoopObjFields(locale)
94+
if err != nil {
95+
return []completions.Completion{}, err
96+
}
97+
98+
return fields, nil
99+
}
100+
60101
func removeTrailingChar(doc string, line, col uint, char byte) string {
61102
lines := strings.Split(doc, "\n")
62103
if int(line) >= len(lines) {

0 commit comments

Comments
 (0)