|
10 | 10 | * Microsoft Corporation - Auto Closing Tags |
11 | 11 | */ |
12 | 12 |
|
13 | | -import { prepareExecutable } from './javaServerStarter'; |
14 | | -import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams, ReferencesRequest, NotificationType, MessageType } from 'vscode-languageclient'; |
15 | | -import * as requirements from './requirements'; |
16 | | -import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration, Uri, extensions, Command } from "vscode"; |
17 | | -import * as path from 'path'; |
18 | 13 | import * as os from 'os'; |
19 | | -import { activateTagClosing, AutoCloseResult } from './tagClosing'; |
| 14 | +import * as path from 'path'; |
| 15 | +import { Command, commands, ExtensionContext, extensions, IndentAction, LanguageConfiguration, languages, Position, TextDocument, Uri, window, workspace, WorkspaceFolder } from "vscode"; |
| 16 | +import { DidChangeConfigurationNotification, LanguageClient, LanguageClientOptions, MessageType, NotificationType, ReferencesRequest, RequestType, RevealOutputChannelOn, TextDocumentPositionParams } from 'vscode-languageclient'; |
20 | 17 | import { Commands } from './commands'; |
21 | | -import { onConfigurationChange, subscribeJDKChangeConfiguration } from './settings'; |
22 | | -import { collectXmlJavaExtensions, onExtensionChange } from './plugin'; |
| 18 | +import { prepareExecutable } from './javaServerStarter'; |
23 | 19 | import { markdownPreviewProvider } from "./markdownPreviewProvider"; |
| 20 | +import { collectXmlJavaExtensions, onExtensionChange } from './plugin'; |
| 21 | +import * as requirements from './requirements'; |
| 22 | +import { onConfigurationChange, subscribeJDKChangeConfiguration } from './settings'; |
| 23 | +import { activateTagClosing, AutoCloseResult } from './tagClosing'; |
24 | 24 |
|
25 | 25 | export interface ScopeInfo { |
26 | 26 | scope: "default" | "global" | "workspace" | "folder"; |
@@ -249,6 +249,13 @@ export function activate(context: ExtensionContext) { |
249 | 249 | })); |
250 | 250 | } |
251 | 251 |
|
| 252 | + // When the current document changes, update ${fileDirname} and ${fileBasenameNoExtension} |
| 253 | + // for the file associations, and send the updated settings to the server |
| 254 | + context.subscriptions.push(window.onDidChangeActiveTextEditor(() => { |
| 255 | + languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getXMLSettings(requirements.java_home) }); |
| 256 | + onConfigurationChange(); |
| 257 | + })); |
| 258 | + |
252 | 259 | const api: XMLExtensionApi = { |
253 | 260 | // add API set catalogs to internal memory |
254 | 261 | addXMLCatalogs: (catalogs: string[]) => { |
@@ -349,11 +356,52 @@ export function activate(context: ExtensionContext) { |
349 | 356 | xml['xml']['catalogs'].push(catalog); |
350 | 357 | } |
351 | 358 | }) |
352 | | - externalXmlSettings.xmlFileAssociations.forEach(element => { |
353 | | - if (!xml['xml']['fileAssociations'].some(fileAssociation => fileAssociation.systemId === element.systemId)) { |
354 | | - xml['xml']['fileAssociations'].push(element); |
| 359 | + const variableSubstitutedAssociations: XMLFileAssociation[] = |
| 360 | + xml['xml']['fileAssociations'].map((association: XMLFileAssociation): XMLFileAssociation => { |
| 361 | + |
| 362 | + const currentFile: TextDocument = window.activeTextEditor.document; |
| 363 | + const currentFileUri: string = currentFile ? currentFile.uri.fsPath : undefined; |
| 364 | + const currentWorkspace: WorkspaceFolder = workspace.getWorkspaceFolder(currentFile && currentFile.uri); |
| 365 | + const currentWorkspaceUri: string = (currentWorkspace && currentWorkspace.uri.fsPath) |
| 366 | + || (workspace.workspaceFolders && workspace.workspaceFolders[0].uri.fsPath); |
| 367 | + |
| 368 | + if (!currentWorkspaceUri |
| 369 | + && (association.pattern.indexOf('&{workspaceFolder}') >= 0 |
| 370 | + || association.systemId.indexOf('&{workspaceFolder}') >= 0)) { |
| 371 | + return; |
355 | 372 | } |
| 373 | + |
| 374 | + if (!currentFileUri |
| 375 | + && (association.pattern.indexOf('&{fileDirname}') >= 0 |
| 376 | + || association.systemId.indexOf('&{fileDirname}') >= 0 |
| 377 | + || association.pattern.indexOf('&{fileBasenameNoExtension}') >= 0 |
| 378 | + || association.systemId.indexOf('&{fileBasenameNoExtension}') >= 0)) { |
| 379 | + return; |
| 380 | + } |
| 381 | + |
| 382 | + /** |
| 383 | + * Returns the string with the values for: |
| 384 | + * * ${workspaceFolder} |
| 385 | + * * ${fileDirname} |
| 386 | + * * ${fileBasenameNoExtension} |
| 387 | + * substituted into the string |
| 388 | + * |
| 389 | + * @param val the value to substitute the variables into |
| 390 | + * @return the string with values for the variables substituted into the string |
| 391 | + */ |
| 392 | + const subVars = (val: string): string => { |
| 393 | + let newVal: string = val.replace(/\$\{workspaceFolder\}/g, currentWorkspaceUri); |
| 394 | + newVal = newVal.replace(/\$\{fileDirname\}/g, path.dirname(currentFileUri)); |
| 395 | + newVal = newVal.replace(/\$\{fileBasenameNoExtension\}/g, path.basename(currentFileUri, path.extname(currentFileUri))); |
| 396 | + return newVal; |
| 397 | + } |
| 398 | + |
| 399 | + return { |
| 400 | + pattern: subVars(association.pattern), |
| 401 | + systemId: subVars(association.systemId) |
| 402 | + }; |
356 | 403 | }); |
| 404 | + xml['xml']['fileAssociations'] = [...variableSubstitutedAssociations]; |
357 | 405 |
|
358 | 406 | return xml; |
359 | 407 | } |
|
0 commit comments