mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-19 07:25:27 +02:00
feat: Add missing FormatTemplate
This commit is contained in:
parent
31c3d755a2
commit
3ffd1f4c4b
69
common/formatting/formatting.go
Normal file
69
common/formatting/formatting.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package formatting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FormatTemplate string
|
||||||
|
|
||||||
|
func (f FormatTemplate) Format(
|
||||||
|
options protocol.FormattingOptions,
|
||||||
|
a ...any,
|
||||||
|
) string {
|
||||||
|
trimTrailingSpace := true
|
||||||
|
|
||||||
|
if shouldTrim, found := options["trimTrailingWhitespace"]; found {
|
||||||
|
trimTrailingSpace = shouldTrim.(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
value := ""
|
||||||
|
value = fmt.Sprintf(
|
||||||
|
string(
|
||||||
|
f.replace("/t", getTab(options)),
|
||||||
|
),
|
||||||
|
a...,
|
||||||
|
)
|
||||||
|
|
||||||
|
if trimTrailingSpace {
|
||||||
|
value = strings.TrimRight(value, " ")
|
||||||
|
value = strings.TrimRight(value, "\t")
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f FormatTemplate) replace(format string, replacement string) FormatTemplate {
|
||||||
|
value := string(f)
|
||||||
|
currentIndex := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
position := strings.Index(value[currentIndex:], format)
|
||||||
|
|
||||||
|
if position == -1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
position = position + currentIndex
|
||||||
|
currentIndex = position
|
||||||
|
|
||||||
|
if position == 0 || value[position-1] != '\\' {
|
||||||
|
value = value[:position] + replacement + value[position+len(format):]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return FormatTemplate(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTab(options protocol.FormattingOptions) string {
|
||||||
|
tabSize := options["tabSize"].(float64)
|
||||||
|
insertSpace := options["insertSpaces"].(bool)
|
||||||
|
|
||||||
|
if insertSpace {
|
||||||
|
return strings.Repeat(" ", int(tabSize))
|
||||||
|
} else {
|
||||||
|
return "\t"
|
||||||
|
}
|
||||||
|
}
|
81
common/formatting/formatting_test.go
Normal file
81
common/formatting/formatting_test.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package formatting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSimpleTabExampleWithTabOptions(
|
||||||
|
t *testing.T,
|
||||||
|
) {
|
||||||
|
template := FormatTemplate("%s/t%s")
|
||||||
|
|
||||||
|
options := protocol.FormattingOptions{
|
||||||
|
"tabSize": float64(4),
|
||||||
|
"insertSpaces": false,
|
||||||
|
}
|
||||||
|
|
||||||
|
result := template.Format(options, "PermitRootLogin", "yes")
|
||||||
|
expected := "PermitRootLogin\tyes"
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %q but got %q", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleTabExampleWithSpaceOptions(
|
||||||
|
t *testing.T,
|
||||||
|
) {
|
||||||
|
template := FormatTemplate("%s/t%s")
|
||||||
|
|
||||||
|
options := protocol.FormattingOptions{
|
||||||
|
"tabSize": float64(4),
|
||||||
|
"insertSpaces": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
result := template.Format(options, "PermitRootLogin", "yes")
|
||||||
|
expected := "PermitRootLogin yes"
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %q but got %q", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleExampleWhiteSpaceAtEndShouldTrim(
|
||||||
|
t *testing.T,
|
||||||
|
) {
|
||||||
|
template := FormatTemplate("%s/t%s")
|
||||||
|
|
||||||
|
options := protocol.FormattingOptions{
|
||||||
|
"tabSize": float64(4),
|
||||||
|
"insertSpaces": false,
|
||||||
|
"trimTrailingWhitespace": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
result := template.Format(options, "PermitRootLogin", "yes ")
|
||||||
|
expected := "PermitRootLogin\tyes"
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %q but got %q", expected, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleExampleWhiteSpaceAtEndShouldNOTTrim(
|
||||||
|
t *testing.T,
|
||||||
|
) {
|
||||||
|
template := FormatTemplate("%s/t%s")
|
||||||
|
|
||||||
|
options := protocol.FormattingOptions{
|
||||||
|
"tabSize": float64(4),
|
||||||
|
"insertSpaces": false,
|
||||||
|
"trimTrailingWhitespace": false,
|
||||||
|
}
|
||||||
|
|
||||||
|
result := template.Format(options, "PermitRootLogin", "yes ")
|
||||||
|
expected := "PermitRootLogin\tyes "
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("Expected %q but got %q", expected, result)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user