1+ import type { LanguageServerConfigurator } from '@arkts/shared'
12import type { Diagnostic , LanguageServicePlugin } from '@volar/language-server'
2- import { DiagnosticSeverity , Range , TextDocument } from '@volar/language-server'
3+ import { DiagnosticSeverity , Range } from '@volar/language-server'
34import { ContextUtil } from '../utils/context-util'
45
5- export function createArkTSLinter ( ets : typeof import ( 'ohos-typescript' ) ) : LanguageServicePlugin {
6+ export function createArkTSLinter ( ets : typeof import ( 'ohos-typescript' ) , config : LanguageServerConfigurator ) : LanguageServicePlugin {
67 return {
78 name : 'arkts-linter' ,
89 capabilities : {
@@ -30,9 +31,13 @@ export function createArkTSLinter(ets: typeof import('ohos-typescript')): Langua
3031 return {
3132 dispose : ( ) => contextUtil . dispose ( ) ,
3233 provideDiagnostics ( document ) {
33- const languageService = contextUtil . getStandaloneLanguageService ( ets )
34+ if ( ! document . getText ( ) . trim ( ) ) return [ ]
35+ const sdkPath = config . getSdkPath ( )
3436 const documentUri = contextUtil . decodeTextDocumentUri ( document )
35- if ( ! languageService || ! documentUri ) return [ ]
37+ if ( ! documentUri || documentUri . fsPath . endsWith ( '.d.ets' ) ) return [ ]
38+ if ( sdkPath && ( ! documentUri || documentUri . fsPath . startsWith ( sdkPath ) ) ) return [ ]
39+ const languageService = contextUtil . getStandaloneLanguageService ( ets )
40+ if ( ! documentUri || ! languageService ) return [ ]
3641
3742 // getBuilderProgram() 在 ArkTS 中其实是支持一个参数: withLinterProgram, 用于指定是否
3843 // 创建包含 linter 程序的 BuilderProgram (即,使用 ets.createIncrementalProgramForArkTs() 函数
@@ -52,12 +57,11 @@ export function createArkTSLinter(ets: typeof import('ohos-typescript')): Langua
5257 if ( ! diagnostic . file ) continue
5358 if ( typeof diagnostic . start !== 'number' || typeof diagnostic . length !== 'number' ) continue
5459 if ( diagnostic . file . fileName !== documentUri . fsPath ) continue
55- const textDocument = TextDocument . create ( diagnostic . file ?. fileName ?? '' , 'ets' , 0 , diagnostic . file ?. getText ( ) ?? '' )
5660
5761 diagnostics . push ( {
5862 range : Range . create (
59- textDocument . positionAt ( diagnostic . start ) ,
60- textDocument . positionAt ( diagnostic . start + diagnostic . length ) ,
63+ document . positionAt ( diagnostic . start ) ,
64+ document . positionAt ( diagnostic . start + diagnostic . length ) ,
6165 ) ,
6266 message : typeof diagnostic . messageText === 'string' ? diagnostic . messageText : diagnostic . messageText . messageText ,
6367 severity : convertDiagnosticCategoryToSeverity ( diagnostic . category ) ,
0 commit comments