From 8ce08c3491a76734c51845974d217fe8af11148c Mon Sep 17 00:00:00 2001 From: francisco souza <108725+fsouza@users.noreply.github.com> Date: Tue, 19 Apr 2022 18:59:25 -0400 Subject: [PATCH] Support skipping textDocument/formatting with requireMarker too Closes #213. --- langserver/handle_text_document_formatting.go | 3 ++ .../handle_text_document_formatting_test.go | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 langserver/handle_text_document_formatting_test.go diff --git a/langserver/handle_text_document_formatting.go b/langserver/handle_text_document_formatting.go index 07f0fd7..d49a163 100644 --- a/langserver/handle_text_document_formatting.go +++ b/langserver/handle_text_document_formatting.go @@ -62,6 +62,9 @@ func (h *langHandler) formatting(uri DocumentURI, options FormattingOptions) ([] if cfgs, ok := h.configs[f.LanguageID]; ok { for _, cfg := range cfgs { if cfg.FormatCommand != "" { + if dir := matchRootPath(fname, cfg.RootMarkers); dir == "" && cfg.RequireMarker { + continue + } configs = append(configs, cfg) } } diff --git a/langserver/handle_text_document_formatting_test.go b/langserver/handle_text_document_formatting_test.go new file mode 100644 index 0000000..af0f33f --- /dev/null +++ b/langserver/handle_text_document_formatting_test.go @@ -0,0 +1,44 @@ +package langserver + +import ( + "log" + "os" + "path/filepath" + "testing" +) + +func TestFormattingRequireRootMatcher(t *testing.T) { + base, _ := os.Getwd() + file := filepath.Join(base, "foo") + uri := toURI(file) + + h := &langHandler{ + logger: log.New(log.Writer(), "", log.LstdFlags), + rootPath: base, + configs: map[string][]Language{ + "vim": { + { + LintCommand: `echo ` + file + `:2:No it is normal!`, + LintIgnoreExitCode: true, + LintStdin: true, + RequireMarker: true, + RootMarkers: []string{".vimlintrc"}, + }, + }, + }, + files: map[DocumentURI]*File{ + uri: { + LanguageID: "vim", + Text: "scriptencoding utf-8\nabnormal!\n", + }, + }, + } + + d, err := h.formatRequest(uri, FormattingOptions{}) + if err != nil { + t.Fatal(err) + } + if len(d) != 0 { + t.Fatal("text edits should be zero as we have no root marker for the language but require one", d) + } +}