fix(lsp): improve code quality in directory diagnostics

- Dedupe directory-checking logic: findWorkspaceRoot now uses isDirectoryPath helper
- Add early return when no files match extension to avoid starting LSP server unnecessarily
This commit is contained in:
acamq
2026-03-08 16:56:33 -06:00
parent 858b10df6f
commit ecdc835b13
2 changed files with 21 additions and 12 deletions

View File

@@ -84,6 +84,15 @@ export async function aggregateDiagnosticsForDirectory(
const wasCapped = allFiles.length > maxFiles
const filesToProcess = allFiles.slice(0, maxFiles)
if (filesToProcess.length === 0) {
return [
`Directory: ${absDir}`,
`Extension: ${extension}`,
`Files scanned: 0`,
`No files found with extension "${extension}".`,
].join("\n")
}
const root = findWorkspaceRoot(absDir)
const allDiagnostics: Diagnostic[] = []

View File

@@ -6,10 +6,21 @@ import { LSPClient, lspManager } from "./client"
import { findServerForExtension } from "./config"
import type { ServerLookupResult } from "./types"
export function isDirectoryPath(filePath: string): boolean {
if (!existsSync(filePath)) {
return false
}
return statSync(filePath).isDirectory()
}
export function uriToPath(uri: string): string {
return fileURLToPath(uri)
}
export function findWorkspaceRoot(filePath: string): string {
let dir = resolve(filePath)
if (!existsSync(dir) || !require("fs").statSync(dir).isDirectory()) {
if (!existsSync(dir) || !isDirectoryPath(dir)) {
dir = require("path").dirname(dir)
}
@@ -29,17 +40,6 @@ export function findWorkspaceRoot(filePath: string): string {
return require("path").dirname(resolve(filePath))
}
export function uriToPath(uri: string): string {
return fileURLToPath(uri)
}
export function isDirectoryPath(filePath: string): boolean {
if (!existsSync(filePath)) {
return false
}
return statSync(filePath).isDirectory()
}
export function formatServerLookupError(result: Exclude<ServerLookupResult, { status: "found" }>): string {
if (result.status === "not_installed") {
const { server, installHint } = result