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) + } +}