-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lsp: Template new empty files & template on format (#1051)
* lsp: Template new empty files & template on format Empty files will be created with a template content instead of being an error. New empty files will be immediately updated with the template content. Some client operations will not trigger the new file event, so in such cases, a save might be required to trigger the template update. Fixes #1048 Signed-off-by: Charlie Egan <[email protected]> * lsp: Add test for server templating Signed-off-by: Charlie Egan <[email protected]> --------- Signed-off-by: Charlie Egan <[email protected]>
- Loading branch information
1 parent
ced7c70
commit bf6e879
Showing
4 changed files
with
197 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package lsp | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/styrainc/regal/internal/lsp/clients" | ||
"github.com/styrainc/regal/internal/lsp/uri" | ||
) | ||
|
||
func TestServerTemplateContentsForFile(t *testing.T) { | ||
t.Parallel() | ||
|
||
s := NewLanguageServer( | ||
&LanguageServerOptions{ | ||
ErrorLog: os.Stderr, | ||
}, | ||
) | ||
|
||
td := t.TempDir() | ||
|
||
filePath := filepath.Join(td, "foo/bar/baz.rego") | ||
regalPath := filepath.Join(td, ".regal/config.yaml") | ||
|
||
initialState := map[string]string{ | ||
filePath: "", | ||
regalPath: "", | ||
} | ||
|
||
// create the initial state needed for the regal config root detection | ||
for file := range initialState { | ||
fileDir := filepath.Dir(file) | ||
|
||
err := os.MkdirAll(fileDir, 0o755) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
err = os.WriteFile(file, []byte(""), 0o600) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
} | ||
|
||
fileURI := uri.FromPath(clients.IdentifierGeneric, filePath) | ||
|
||
s.cache.SetFileContents(fileURI, "") | ||
|
||
newContents, err := s.templateContentsForFile(fileURI) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if newContents != "package foo.bar\n\nimport rego.v1\n" { | ||
t.Fatalf("unexpected contents: %v", newContents) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters