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:
@@ -84,6 +84,15 @@ export async function aggregateDiagnosticsForDirectory(
|
|||||||
const wasCapped = allFiles.length > maxFiles
|
const wasCapped = allFiles.length > maxFiles
|
||||||
const filesToProcess = allFiles.slice(0, 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 root = findWorkspaceRoot(absDir)
|
||||||
|
|
||||||
const allDiagnostics: Diagnostic[] = []
|
const allDiagnostics: Diagnostic[] = []
|
||||||
|
|||||||
@@ -6,10 +6,21 @@ import { LSPClient, lspManager } from "./client"
|
|||||||
import { findServerForExtension } from "./config"
|
import { findServerForExtension } from "./config"
|
||||||
import type { ServerLookupResult } from "./types"
|
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 {
|
export function findWorkspaceRoot(filePath: string): string {
|
||||||
let dir = resolve(filePath)
|
let dir = resolve(filePath)
|
||||||
|
|
||||||
if (!existsSync(dir) || !require("fs").statSync(dir).isDirectory()) {
|
if (!existsSync(dir) || !isDirectoryPath(dir)) {
|
||||||
dir = require("path").dirname(dir)
|
dir = require("path").dirname(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,17 +40,6 @@ export function findWorkspaceRoot(filePath: string): string {
|
|||||||
return require("path").dirname(resolve(filePath))
|
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 {
|
export function formatServerLookupError(result: Exclude<ServerLookupResult, { status: "found" }>): string {
|
||||||
if (result.status === "not_installed") {
|
if (result.status === "not_installed") {
|
||||||
const { server, installHint } = result
|
const { server, installHint } = result
|
||||||
|
|||||||
Reference in New Issue
Block a user