ollie/tools/lsp/workspace.go

30 lines
585 B
Go

package lsp
import (
"os"
"path/filepath"
)
// FindRoot walks up from filepath looking for any marker file/dir.
// Returns the directory containing the first marker found, or fallback.
func FindRoot(file string, markers []string, fallback string) string {
dir := filepath.Dir(filepath.Clean(file))
for {
for _, m := range markers {
if _, err := os.Stat(filepath.Join(dir, m)); err == nil {
return dir
}
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
if fallback != "" {
return fallback
}
cwd, _ := os.Getwd()
return cwd
}