-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f565bd
commit e37f35a
Showing
6 changed files
with
154 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
extensions/html-language-features/server/src/modes/project.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ServerProject } from '@volar/language-server'; | ||
import { ServerContext, ServerOptions } from '@volar/language-server/lib/server'; | ||
import { LanguageService, ServiceEnvironment, ServicePlugin, createLanguageService } from '@volar/language-service'; | ||
import type { SnapshotDocument } from '@volar/snapshot-document'; | ||
import { createLanguage, createSys } from '@volar/typescript'; | ||
import { createProjectHost } from './projectHost'; | ||
|
||
export async function createProject( | ||
context: ServerContext, | ||
serviceEnv: ServiceEnvironment, | ||
getLanguagePlugins: ServerOptions['getLanguagePlugins'], | ||
servicePlugins: ServicePlugin[], | ||
getCurrentTextDocument: () => SnapshotDocument, | ||
): Promise<ServerProject> { | ||
|
||
let languageService: LanguageService | undefined; | ||
|
||
const ts = context.ts!; | ||
const sys = createSys(ts, serviceEnv, ''); | ||
const host = createProjectHost( | ||
serviceEnv.typescript!, | ||
fileName => sys.readFile(fileName), | ||
getCurrentTextDocument, | ||
() => getCurrentTextDocument().getSnapshot(), | ||
context.tsLocalized, | ||
) | ||
const languagePlugins = await getLanguagePlugins(serviceEnv, { | ||
typescript: { | ||
configFileName: undefined, | ||
host, | ||
sys, | ||
}, | ||
}); | ||
|
||
return { | ||
serviceEnv, | ||
getLanguageService, | ||
getLanguageServiceDontCreate: () => languageService, | ||
dispose, | ||
}; | ||
|
||
function getLanguageService() { | ||
if (!languageService) { | ||
const language = createLanguage( | ||
ts, | ||
sys, | ||
languagePlugins, | ||
undefined, | ||
host, | ||
{ | ||
fileNameToFileId: serviceEnv.typescript!.fileNameToUri, | ||
fileIdToFileName: serviceEnv.typescript!.uriToFileName, | ||
}, | ||
); | ||
languageService = createLanguageService( | ||
language, | ||
servicePlugins, | ||
serviceEnv, | ||
); | ||
} | ||
return languageService; | ||
} | ||
|
||
function dispose() { | ||
sys.dispose(); | ||
languageService?.dispose(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
extensions/html-language-features/server/src/modes/projectHost.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { ServiceEnvironment, TypeScriptProjectHost, resolveCommonLanguageId, TextDocument } from '@volar/language-service'; | ||
import type * as ts from 'typescript'; | ||
import { JQUERY_PATH } from './javascriptLibs'; | ||
|
||
export function createProjectHost( | ||
{ uriToFileName, fileNameToUri }: NonNullable<ServiceEnvironment['typescript']>, | ||
readFile: (fileName: string) => string | undefined, | ||
getCurrentTextDocument: () => TextDocument, | ||
getCurrentDocumentSnapshot: () => ts.IScriptSnapshot, | ||
tsLocalized?: ts.MapLike<string>, | ||
) { | ||
const libSnapshots = new Map<string, ts.IScriptSnapshot | undefined>(); | ||
const compilerOptions: ts.CompilerOptions = { allowNonTsExtensions: true, allowJs: true, lib: ['lib.es2020.full.d.ts'], target: 99 satisfies ts.ScriptTarget.Latest, moduleResolution: 1 satisfies ts.ModuleResolutionKind.Classic, experimentalDecorators: false }; | ||
const host: TypeScriptProjectHost = { | ||
getCompilationSettings: () => compilerOptions, | ||
getScriptFileNames: () => [uriToFileName(getCurrentTextDocument().uri), JQUERY_PATH], | ||
getCurrentDirectory: () => '', | ||
getProjectVersion: () => getCurrentTextDocument().uri + ',' + getCurrentTextDocument().version.toString(), | ||
getScriptSnapshot: fileName => { | ||
if (fileNameToUri(fileName) === getCurrentTextDocument().uri) { | ||
return getCurrentDocumentSnapshot(); | ||
} | ||
else { | ||
let snapshot = libSnapshots.get(fileName); | ||
if (!snapshot) { | ||
const text = readFile(fileName); | ||
if (text !== undefined) { | ||
snapshot = { | ||
getText: (start, end) => text.substring(start, end), | ||
getLength: () => text.length, | ||
getChangeRange: () => undefined, | ||
}; | ||
} | ||
libSnapshots.set(fileName, snapshot); | ||
} | ||
return snapshot; | ||
} | ||
}, | ||
getLocalizedDiagnosticMessages: tsLocalized ? () => tsLocalized : undefined, | ||
getLanguageId: uri => { | ||
if (uri === getCurrentTextDocument().uri) { | ||
return getCurrentTextDocument().languageId; | ||
} | ||
return resolveCommonLanguageId(uri) | ||
}, | ||
}; | ||
return host; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
extensions/html-language-features/server/src/modes/servicePlugins.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { ServicePlugin } from '@volar/language-service'; | ||
import { create as createCssServicePlugin } from 'volar-service-css'; | ||
import { create as createHtmlServicePlugin } from 'volar-service-html'; | ||
import { create as createTypeScriptServicePlugin } from 'volar-service-typescript'; | ||
|
||
export function getLanguageServices(ts: typeof import('typescript')) { | ||
const html1ServicePlugins: ServicePlugin[] = [ | ||
createCssServicePlugin(), | ||
createHtmlServicePlugin(), | ||
createTypeScriptServicePlugin(ts), | ||
{ | ||
create() { | ||
return { | ||
resolveEmbeddedCodeFormattingOptions(code, options) { | ||
if (code.id.startsWith('css_')) { | ||
options.initialIndentLevel++; | ||
} | ||
return options; | ||
}, | ||
}; | ||
}, | ||
}, | ||
]; | ||
return html1ServicePlugins; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters