Skip to content

Commit 4e575f5

Browse files
committed
fix: Doing some cleanup
1 parent 89a3813 commit 4e575f5

File tree

7 files changed

+213
-216
lines changed

7 files changed

+213
-216
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ If you would like to contribute, please read the [contributing documentation](./
2020

2121
## Special Thanks
2222

23-
A special thanks also goes to [jowharshamshiri](https://github.com/jowharshamshiri) for getting the ball rolling on [tg-hcl-lsp](https://github.com/jowharshamshiri/tg-hcl-lsp), the first community supported Terragrunt Language Server. Seeing the community commit to creating a Language Server on their own convinced the maintainers of Terragrunt to create this official version.
23+
A special thanks also goes to [jowharshamshiri](https://github.com/jowharshamshiri) for getting the ball rolling on [tg-hcl-lsp](https://github.com/jowharshamshiri/tg-hcl-lsp) and [mightyguava](https://github.com/mightyguava) for [terragrunt-langserver](https://github.com/mightyguava/terragrunt-langserver), the first community supported Terragrunt Language Servers. Seeing the community commit to creating Language Servers on their own convinced the maintainers of Terragrunt to create and maintain this official version.
2424

2525
This one was created by learning from [tjdevries](https://github.com/tjdevries)'s [educational-lsp](https://github.com/tjdevries/educationalsp), and that educational example served as a great Golang-based starting point for this project.
2626

internal/logger/logger.go

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,33 @@ type Logger struct {
1818
// When supplied with a filename, it'll create a new file and write logs to it.
1919
// Otherwise, it'll write logs to stderr.
2020
func NewLogger(filename string) *Logger {
21-
var (
22-
logWriter io.Writer
23-
closer io.Closer
24-
)
25-
26-
if filename != "" {
27-
const readWritePerm = 0666
28-
29-
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, readWritePerm)
30-
if err != nil {
31-
slog.Error("Failed to open log file", "error", err)
32-
os.Exit(1)
21+
if filename == "" {
22+
handler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
23+
Level: slog.LevelInfo,
24+
})
25+
logger := slog.New(handler)
26+
27+
return &Logger{
28+
Logger: logger,
3329
}
30+
}
3431

35-
logWriter = file
36-
closer = file
37-
} else {
38-
logWriter = os.Stderr
39-
closer = nil
32+
const readWritePerm = 0666
33+
34+
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, readWritePerm)
35+
if err != nil {
36+
slog.Error("Failed to open log file", "error", err)
37+
os.Exit(1)
4038
}
4139

42-
// Create a JSON handler for structured logging
43-
handler := slog.NewJSONHandler(logWriter, &slog.HandlerOptions{
40+
handler := slog.NewJSONHandler(file, &slog.HandlerOptions{
4441
Level: slog.LevelDebug,
4542
})
46-
4743
logger := slog.New(handler)
4844

4945
return &Logger{
5046
Logger: logger,
51-
closer: closer,
47+
closer: file,
5248
}
5349
}
5450

@@ -62,31 +58,16 @@ func (l *Logger) Close() error {
6258
}
6359

6460
// Debug logs a debug message
65-
func (l *Logger) Debug(args ...interface{}) {
66-
l.Logger.Debug("debug", "msg", args)
67-
}
68-
69-
// Debugf logs a formatted debug message
70-
func (l *Logger) Debugf(format string, args ...interface{}) {
71-
l.Logger.Debug(format, args...)
61+
func (l *Logger) Debug(msg string, args ...interface{}) {
62+
l.Logger.Debug(msg, args...)
7263
}
7364

7465
// Info logs an info message
75-
func (l *Logger) Info(args ...interface{}) {
76-
l.Logger.Info("info", "msg", args)
77-
}
78-
79-
// Infof logs a formatted info message
80-
func (l *Logger) Infof(format string, args ...interface{}) {
81-
l.Logger.Info(format, args...)
66+
func (l *Logger) Info(msg string, args ...interface{}) {
67+
l.Logger.Info(msg, args...)
8268
}
8369

8470
// Error logs an error message
85-
func (l *Logger) Error(args ...interface{}) {
86-
l.Logger.Error("error", "msg", args)
87-
}
88-
89-
// Errorf logs a formatted error message
90-
func (l *Logger) Errorf(format string, args ...interface{}) {
91-
l.Logger.Error(format, args...)
71+
func (l *Logger) Error(msg string, args ...interface{}) {
72+
l.Logger.Error(msg, args...)
9273
}

internal/tg/definition/definition.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ func GetDefinitionTargetWithContext(l *logger.Logger, store store.Store, positio
5050
// Identify configuration blocks
5151
block, labels, isBlock := isConfigBlockLine(line)
5252
if isBlock {
53-
l.Debugf("Found block: %s", block)
53+
l.Debug(
54+
"Found block",
55+
"block", block,
56+
"labels", labels,
57+
"line", scannedLines,
58+
)
5459

5560
if block == DefinitionContextInclude {
5661
definitionContext = DefinitionContextInclude
@@ -64,7 +69,11 @@ func GetDefinitionTargetWithContext(l *logger.Logger, store store.Store, positio
6469

6570
// Check if the current line is the one we're looking for
6671
if scannedLines == int(position.Line) {
67-
l.Debugf("Hit line %d: %s", position.Line, line)
72+
l.Debug(
73+
"Hit line",
74+
"line", scannedLines,
75+
"position", position.Line,
76+
)
6877

6978
lineHit = true
7079
}
@@ -78,10 +87,18 @@ func GetDefinitionTargetWithContext(l *logger.Logger, store store.Store, positio
7887
// The reason we do both checks is that we need to
7988
// account for single line block definitions.
8089
if line == "}" || (strings.HasSuffix(line, "}") && isBlock) {
81-
l.Debugf("End of block: %s, line: %d", block, scannedLines)
90+
l.Debug(
91+
"End of block",
92+
"block", block,
93+
"line", scannedLines,
94+
)
8295

8396
if lineHit && definitionContext != "" {
84-
l.Debugf("Found target: %s, context: %s", target, definitionContext)
97+
l.Debug(
98+
"Found target",
99+
"target", target,
100+
"context", definitionContext,
101+
)
85102

86103
return target, definitionContext
87104
}
@@ -93,7 +110,11 @@ func GetDefinitionTargetWithContext(l *logger.Logger, store store.Store, positio
93110
scannedLines++
94111
}
95112

96-
l.Debugf("No target found at %d:%d", position.Line, position.Character)
113+
l.Debug(
114+
"No target found",
115+
"line", position.Line,
116+
"character", position.Character,
117+
)
97118

98119
return "", DefinitionContextNull
99120
}

internal/tg/definition/definition_test.go

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,3 @@
1-
// // Package definition provides the logic for finding
2-
// // definitions in Terragrunt configurations.
3-
// package definition
4-
//
5-
// import (
6-
// "bufio"
7-
// "strings"
8-
// "terragrunt-ls/internal/tg/store"
9-
//
10-
// "go.lsp.dev/protocol"
11-
// "go.uber.org/zap"
12-
// )
13-
//
14-
// const (
15-
// // DefinitionContextInclude is the context for an include definition.
16-
// // This means that the user is trying to find the definition of an include.
17-
// DefinitionContextInclude = "include"
18-
//
19-
// // DefinitionContextNull is the context for a null definition.
20-
// // This means that the user is trying to go to the definition of nothing useful.
21-
// DefinitionContextNull = "null"
22-
// )
23-
//
24-
// func GetDefinitionTargetWithContext(l *zap.SugaredLogger, store store.Store, position protocol.Position) (string, string) {
25-
// document := store.Document
26-
//
27-
// scanner := bufio.NewScanner(strings.NewReader(document))
28-
// target := ""
29-
// definitionContext := ""
30-
// lineHit := false
31-
// scannedLines := 0
32-
//
33-
// for scanner.Scan() {
34-
// line := scanner.Text()
35-
//
36-
// // Trim whitespace early to avoid
37-
// // having to do it later.
38-
// line = strings.TrimSpace(line)
39-
//
40-
// // Skip empty lines
41-
// if len(line) == 0 {
42-
// continue
43-
// }
44-
//
45-
// // Skip comments
46-
// if strings.HasPrefix(line, "#") || strings.HasPrefix(line, "//") {
47-
// continue
48-
// }
49-
//
50-
// // Identify configuration blocks
51-
// block, labels, isBlock := isConfigBlockLine(line)
52-
// if isBlock {
53-
// l.Debugf("Found block: %s", block)
54-
//
55-
// if block == DefinitionContextInclude {
56-
// definitionContext = DefinitionContextInclude
57-
//
58-
// // Includes can have zero labels
59-
// if len(labels) > 0 {
60-
// target = labels[0]
61-
// }
62-
// }
63-
// }
64-
//
65-
// // Check if the current line is the one we're looking for
66-
// if scannedLines == int(position.Line) {
67-
// l.Debugf("Hit line %d: %s", position.Line, line)
68-
//
69-
// lineHit = true
70-
// }
71-
//
72-
// // Check if we've reached the end of the block.
73-
// //
74-
// // End of blocks are special, as we've either discovered
75-
// // the context for our definition, or we've reached
76-
// // a point where we need to reset the context.
77-
// //
78-
// // The reason we do both checks is that we need to
79-
// // account for single line block definitions.
80-
// if line == "}" || (strings.HasSuffix(line, "}") && isBlock) {
81-
// l.Debugf("End of block: %s, line: %d", block, scannedLines)
82-
//
83-
// if lineHit && definitionContext != "" {
84-
// l.Debugf("Found target: %s, context: %s", target, definitionContext)
85-
//
86-
// return target, definitionContext
87-
// }
88-
//
89-
// definitionContext = ""
90-
// target = ""
91-
// }
92-
//
93-
// scannedLines++
94-
// }
95-
//
96-
// l.Debugf("No target found at %d:%d", position.Line, position.Character)
97-
//
98-
// return "", DefinitionContextNull
99-
// }
100-
//
101-
// func isConfigBlockLine(line string) (string, []string, bool) {
102-
// fields := strings.Fields(line)
103-
//
104-
// const minConfigBlockLen = 2
105-
//
106-
// if len(fields) < minConfigBlockLen {
107-
// return "", nil, false
108-
// }
109-
//
110-
// block := fields[0]
111-
// labels := []string{}
112-
//
113-
// for _, field := range fields[1:] {
114-
// if field == "=" {
115-
// return "", nil, false
116-
// }
117-
//
118-
// if field == "{" {
119-
// return block, labels, true
120-
// }
121-
//
122-
// labels = append(labels, strings.Trim(field, "\""))
123-
// }
124-
//
125-
// return "", nil, false
126-
// }
127-
1281
package definition_test
1292

1303
import (

internal/tg/hover/hover.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ const (
2323
func GetHoverTargetWithContext(l *logger.Logger, store store.Store, position protocol.Position) (string, string) {
2424
word := text.GetCursorWord(store.Document, position)
2525
if len(word) == 0 {
26-
l.Debugf("No word found at %d:%d", position.Line, position.Character)
26+
l.Debug(
27+
"No word found",
28+
"line", position.Line,
29+
"character", position.Character,
30+
)
2731

2832
return word, HoverContextNull
2933
}
@@ -33,13 +37,23 @@ func GetHoverTargetWithContext(l *logger.Logger, store store.Store, position pro
3337
const localPartsLen = 2
3438

3539
if len(splitExpression) != localPartsLen {
36-
l.Debugf("Invalid word found at %d:%d: %s", position.Line, position.Character, word)
40+
l.Debug(
41+
"Invalid word found",
42+
"line", position.Line,
43+
"character", position.Character,
44+
"word", word,
45+
)
3746

3847
return word, HoverContextNull
3948
}
4049

4150
if splitExpression[0] == "local" {
42-
l.Debugf("Found local variable: %s", splitExpression[1])
51+
l.Debug(
52+
"Found local variable",
53+
"line", position.Line,
54+
"character", position.Character,
55+
"local", splitExpression[1],
56+
)
4357

4458
return splitExpression[1], HoverContextLocal
4559
}

0 commit comments

Comments
 (0)