CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes45downloads
NativeLspClient.ts302 linesDownload Raw Back to lsp
1/**2 * @license3 * Copyright 2025 Qwen Team4 * SPDX-License-Identifier: Apache-2.05 */6 7/**8 * NativeLspClient is an adapter that implements the LspClient interface9 * by delegating all calls to NativeLspService.10 *11 * This class bridges the gap between the generic LspClient interface (defined in core)12 * and the NativeLspService implementation.13 */14 15import type {16  LspCallHierarchyIncomingCall,17  LspCallHierarchyItem,18  LspCallHierarchyOutgoingCall,19  LspClient,20  LspCodeAction,21  LspCodeActionContext,22  LspDefinition,23  LspDiagnostic,24  LspFileDiagnostics,25  LspHoverResult,26  LspLocation,27  LspRange,28  LspReference,29  LspStatusSnapshot,30  LspSymbolInformation,31  LspWorkspaceEdit,32  LspServerStatusInfo,33  LspServiceReinitializeResult,34} from './types.js';35 36import type { NativeLspService } from './NativeLspService.js';37 38function getErrorMessage(error: unknown): string | undefined {39  if (error === undefined || error === null) {40    return undefined;41  }42  return error instanceof Error ? error.message : String(error);43}44 45/**46 * Adapter class that implements LspClient by delegating to NativeLspService.47 *48 * @example49 * ```typescript50 * const lspService = new NativeLspService(config, workspaceContext, ...);51 * await lspService.start();52 * const lspClient = new NativeLspClient(lspService);53 * config.setLspClient(lspClient);54 * ```55 */56export class NativeLspClient implements LspClient {57  /**58   * Creates a new NativeLspClient instance.59   *60   * @param service - The NativeLspService instance to delegate calls to61   */62  constructor(private readonly service: NativeLspService) {}63 64  /**65   * Get the status of all configured LSP servers.66   */67  getServerStatus(): LspServerStatusInfo[] {68    const handles = this.service.getServerHandles();69    const result: LspServerStatusInfo[] = [];70 71    for (const [name, handle] of handles) {72      const error = getErrorMessage(handle.error);73      result.push({74        name,75        status: handle.status,76        command: handle.config.command,77        languages: handle.config.languages ?? [],78        ...(error !== undefined ? { error } : {}),79      });80    }81 82    return result;83  }84 85  reinitialize(): Promise<LspServiceReinitializeResult> {86    return this.service.reinitialize();87  }88 89  /**90   * Search for symbols across the workspace.91   *92   * @param query - The search query string93   * @param limit - Maximum number of results to return94   * @returns Promise resolving to array of symbol information95   */96  workspaceSymbols(97    query: string,98    limit?: number,99  ): Promise<LspSymbolInformation[]> {100    return this.service.workspaceSymbols(query, limit);101  }102 103  /**104   * Find where a symbol is defined.105   *106   * @param location - The source location to find definitions for107   * @param serverName - Optional specific LSP server to query108   * @param limit - Maximum number of results to return109   * @returns Promise resolving to array of definition locations110   */111  definitions(112    location: LspLocation,113    serverName?: string,114    limit?: number,115  ): Promise<LspDefinition[]> {116    return this.service.definitions(location, serverName, limit);117  }118 119  /**120   * Find all references to a symbol.121   *122   * @param location - The source location to find references for123   * @param serverName - Optional specific LSP server to query124   * @param includeDeclaration - Whether to include the declaration in results125   * @param limit - Maximum number of results to return126   * @returns Promise resolving to array of reference locations127   */128  references(129    location: LspLocation,130    serverName?: string,131    includeDeclaration?: boolean,132    limit?: number,133  ): Promise<LspReference[]> {134    return this.service.references(135      location,136      serverName,137      includeDeclaration,138      limit,139    );140  }141 142  /**143   * Get hover information (documentation, type info) for a symbol.144   *145   * @param location - The source location to get hover info for146   * @param serverName - Optional specific LSP server to query147   * @returns Promise resolving to hover result or null if not available148   */149  hover(150    location: LspLocation,151    serverName?: string,152  ): Promise<LspHoverResult | null> {153    return this.service.hover(location, serverName);154  }155 156  /**157   * Get all symbols in a document.158   *159   * @param uri - The document URI to get symbols for160   * @param serverName - Optional specific LSP server to query161   * @param limit - Maximum number of results to return162   * @returns Promise resolving to array of symbol information163   */164  documentSymbols(165    uri: string,166    serverName?: string,167    limit?: number,168  ): Promise<LspSymbolInformation[]> {169    return this.service.documentSymbols(uri, serverName, limit);170  }171 172  /**173   * Find implementations of an interface or abstract method.174   *175   * @param location - The source location to find implementations for176   * @param serverName - Optional specific LSP server to query177   * @param limit - Maximum number of results to return178   * @returns Promise resolving to array of implementation locations179   */180  implementations(181    location: LspLocation,182    serverName?: string,183    limit?: number,184  ): Promise<LspDefinition[]> {185    return this.service.implementations(location, serverName, limit);186  }187 188  /**189   * Prepare call hierarchy item at a position (functions/methods).190   *191   * @param location - The source location to prepare call hierarchy for192   * @param serverName - Optional specific LSP server to query193   * @param limit - Maximum number of results to return194   * @returns Promise resolving to array of call hierarchy items195   */196  prepareCallHierarchy(197    location: LspLocation,198    serverName?: string,199    limit?: number,200  ): Promise<LspCallHierarchyItem[]> {201    return this.service.prepareCallHierarchy(location, serverName, limit);202  }203 204  /**205   * Find all functions/methods that call the given function.206   *207   * @param item - The call hierarchy item to find callers for208   * @param serverName - Optional specific LSP server to query209   * @param limit - Maximum number of results to return210   * @returns Promise resolving to array of incoming calls211   */212  incomingCalls(213    item: LspCallHierarchyItem,214    serverName?: string,215    limit?: number,216  ): Promise<LspCallHierarchyIncomingCall[]> {217    return this.service.incomingCalls(item, serverName, limit);218  }219 220  /**221   * Find all functions/methods called by the given function.222   *223   * @param item - The call hierarchy item to find callees for224   * @param serverName - Optional specific LSP server to query225   * @param limit - Maximum number of results to return226   * @returns Promise resolving to array of outgoing calls227   */228  outgoingCalls(229    item: LspCallHierarchyItem,230    serverName?: string,231    limit?: number,232  ): Promise<LspCallHierarchyOutgoingCall[]> {233    return this.service.outgoingCalls(item, serverName, limit);234  }235 236  /**237   * Get diagnostics for a specific document.238   *239   * @param uri - The document URI to get diagnostics for240   * @param serverName - Optional specific LSP server to query241   * @returns Promise resolving to array of diagnostics242   */243  diagnostics(uri: string, serverName?: string): Promise<LspDiagnostic[]> {244    return this.service.diagnostics(uri, serverName);245  }246 247  /**248   * Get diagnostics for all open documents in the workspace.249   *250   * @param serverName - Optional specific LSP server to query251   * @param limit - Maximum number of file diagnostics to return252   * @returns Promise resolving to array of file diagnostics253   */254  workspaceDiagnostics(255    serverName?: string,256    limit?: number,257  ): Promise<LspFileDiagnostics[]> {258    return this.service.workspaceDiagnostics(serverName, limit);259  }260 261  /**262   * Get code actions available at a specific location.263   *264   * @param uri - The document URI265   * @param range - The range to get code actions for266   * @param context - The code action context including diagnostics267   * @param serverName - Optional specific LSP server to query268   * @param limit - Maximum number of code actions to return269   * @returns Promise resolving to array of code actions270   */271  codeActions(272    uri: string,273    range: LspRange,274    context: LspCodeActionContext,275    serverName?: string,276    limit?: number,277  ): Promise<LspCodeAction[]> {278    return this.service.codeActions(uri, range, context, serverName, limit);279  }280 281  /**282   * Apply a workspace edit (from code action or other sources).283   *284   * @param edit - The workspace edit to apply285   * @param serverName - Optional specific LSP server context286   * @returns Promise resolving to true if edit was applied successfully287   */288  applyWorkspaceEdit(289    edit: LspWorkspaceEdit,290    serverName?: string,291  ): Promise<boolean> {292    return this.service.applyWorkspaceEdit(edit, serverName);293  }294 295  /**296   * Get a point-in-time status snapshot for UI and debug logging.297   */298  getStatusSnapshot(): LspStatusSnapshot {299    return this.service.getStatusSnapshot();300  }301}302 
basant307/AI_Governance_Project · CoolFace