|
11 | 11 | */ |
12 | 12 |
|
13 | 13 | import { prepareExecutable } from './javaServerStarter'; |
14 | | -import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams, ReferencesRequest, NotificationType, MessageType } from 'vscode-languageclient'; |
| 14 | +import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams, ReferencesRequest, NotificationType, MessageType, ShowMessageNotification } from 'vscode-languageclient'; |
15 | 15 | import * as requirements from './requirements'; |
16 | 16 | import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration, Uri, extensions, Command } from "vscode"; |
17 | 17 | import * as path from 'path'; |
@@ -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,50 @@ 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: string = window.activeTextEditor.document.uri.fsPath; |
| 363 | + let currentWorkspace: string = workspace.getWorkspaceFolder(window.activeTextEditor.document.uri).uri.fsPath |
| 364 | + || workspace.workspaceFolders[0].uri.fsPath; |
| 365 | + |
| 366 | + if (!currentWorkspace |
| 367 | + && (association.pattern.indexOf('&{workspaceFolder}') >= 0 |
| 368 | + || association.systemId.indexOf('&{workspaceFolder}') >= 0)) { |
| 369 | + return; |
| 370 | + } |
| 371 | + |
| 372 | + if (!currentFile |
| 373 | + && (association.pattern.indexOf('&{fileDirname}') >= 0 |
| 374 | + || association.systemId.indexOf('&{fileDirname}') >= 0 |
| 375 | + || association.pattern.indexOf('&{fileBasenameNoExtension}') >= 0 |
| 376 | + || association.systemId.indexOf('&{fileBasenameNoExtension}') >= 0)) { |
| 377 | + return; |
| 378 | + } |
| 379 | + |
| 380 | + /** |
| 381 | + * Returns the string with the values for: |
| 382 | + * * ${workspaceFolder} |
| 383 | + * * ${fileDirname} |
| 384 | + * * ${fileBasenameNoExtension} |
| 385 | + * substituted into the string |
| 386 | + * |
| 387 | + * @param val the value to substitute the variables into |
| 388 | + * @return the string with values for the variables subtituted into the string |
| 389 | + */ |
| 390 | + const subVars = (val: string): string => { |
| 391 | + let newVal: string = val.replace(/\$\{workspaceFolder\}/g, currentWorkspace); |
| 392 | + newVal = newVal.replace(/\$\{fileDirname\}/g, path.dirname(currentFile)); |
| 393 | + newVal = newVal.replace(/\$\{fileBasenameNoExtension\}/g, path.basename(currentFile, path.extname(currentFile))); |
| 394 | + return newVal; |
355 | 395 | } |
| 396 | + |
| 397 | + return { |
| 398 | + pattern: subVars(association.pattern), |
| 399 | + systemId: subVars(association.systemId) |
| 400 | + }; |
356 | 401 | }); |
| 402 | + xml['xml']['fileAssociations'] = [...variableSubstitutedAssociations]; |
357 | 403 |
|
358 | 404 | return xml; |
359 | 405 | } |
|
0 commit comments