|
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 { getXMLConfiguration, 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,16 @@ 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, (but only if these variables are referenced), |
| 254 | + // and send the updated settings to the server |
| 255 | + context.subscriptions.push(window.onDidChangeActiveTextEditor(() => { |
| 256 | + if (fileAssocReferencesCurrentFile()) { |
| 257 | + languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getXMLSettings(requirements.java_home) }); |
| 258 | + onConfigurationChange(); |
| 259 | + } |
| 260 | + })); |
| 261 | + |
252 | 262 | const api: XMLExtensionApi = { |
253 | 263 | // add API set catalogs to internal memory |
254 | 264 | addXMLCatalogs: (catalogs: string[]) => { |
@@ -349,14 +359,70 @@ export function activate(context: ExtensionContext) { |
349 | 359 | xml['xml']['catalogs'].push(catalog); |
350 | 360 | } |
351 | 361 | }) |
352 | | - externalXmlSettings.xmlFileAssociations.forEach(element => { |
353 | | - if (!xml['xml']['fileAssociations'].some(fileAssociation => fileAssociation.systemId === element.systemId)) { |
354 | | - xml['xml']['fileAssociations'].push(element); |
355 | | - } |
356 | | - }); |
| 362 | + const variableSubstitutedAssociations: XMLFileAssociation[] = |
| 363 | + xml['xml']['fileAssociations'].map((association: XMLFileAssociation): XMLFileAssociation => { |
| 364 | + |
| 365 | + const currentFile: TextDocument = window.activeTextEditor.document; |
| 366 | + const currentFileUri: string = currentFile && currentFile.uri.fsPath; |
| 367 | + const currentWorkspace: WorkspaceFolder = workspace.getWorkspaceFolder(currentFile && currentFile.uri); |
| 368 | + const currentWorkspaceUri: string = (currentWorkspace && currentWorkspace.uri.fsPath) |
| 369 | + || (workspace.workspaceFolders && workspace.workspaceFolders[0].uri.fsPath); |
| 370 | + |
| 371 | + if (!currentWorkspaceUri |
| 372 | + && (association.pattern.indexOf('&{workspaceFolder}') >= 0 |
| 373 | + || association.systemId.indexOf('&{workspaceFolder}') >= 0)) { |
| 374 | + return; |
| 375 | + } |
| 376 | + |
| 377 | + if (!currentFileUri |
| 378 | + && (association.pattern.indexOf('&{fileDirname}') >= 0 |
| 379 | + || association.systemId.indexOf('&{fileDirname}') >= 0 |
| 380 | + || association.pattern.indexOf('&{fileBasenameNoExtension}') >= 0 |
| 381 | + || association.systemId.indexOf('&{fileBasenameNoExtension}') >= 0)) { |
| 382 | + return; |
| 383 | + } |
| 384 | + |
| 385 | + /** |
| 386 | + * Returns the string with the values for: |
| 387 | + * * ${workspaceFolder} |
| 388 | + * * ${fileDirname} |
| 389 | + * * ${fileBasenameNoExtension} |
| 390 | + * substituted into the string |
| 391 | + * |
| 392 | + * @param val the value to substitute the variables into |
| 393 | + * @return the string with values for the variables substituted into the string |
| 394 | + */ |
| 395 | + const subVars = (val: string): string => { |
| 396 | + let newVal: string = val.replace(/\$\{workspaceFolder\}/g, currentWorkspaceUri); |
| 397 | + newVal = newVal.replace(/\$\{fileDirname\}/g, path.dirname(currentFileUri)); |
| 398 | + newVal = newVal.replace(/\$\{fileBasenameNoExtension\}/g, path.basename(currentFileUri, path.extname(currentFileUri))); |
| 399 | + return newVal; |
| 400 | + } |
| 401 | + |
| 402 | + return { |
| 403 | + pattern: subVars(association.pattern), |
| 404 | + systemId: subVars(association.systemId) |
| 405 | + }; |
| 406 | + }); |
| 407 | + xml['xml']['fileAssociations'] = [...variableSubstitutedAssociations]; |
357 | 408 |
|
358 | 409 | return xml; |
359 | 410 | } |
| 411 | + |
| 412 | + /** |
| 413 | + * Returns true if the the XML settings contain a file association with a variable reference to either ${fileDirname} or ${fileBasenameNoExtension} and false otherwise |
| 414 | + * |
| 415 | + * @return true if the the XML settings contain a file association with a variable reference to either ${fileDirname} or ${fileBasenameNoExtension} and false otherwise |
| 416 | + */ |
| 417 | + function fileAssocReferencesCurrentFile(): boolean { |
| 418 | + for (const assoc of getXMLConfiguration().get('fileAssociations') as XMLFileAssociation[]) { |
| 419 | + if (assoc.pattern.indexOf('${fileDirname}') >= 0 || assoc.pattern.indexOf('${fileBasenameNoExtension}') >= 0 |
| 420 | + || assoc.systemId.indexOf('${fileDirname}') >= 0 || assoc.systemId.indexOf('${fileBasenameNoExtension}') >= 0) { |
| 421 | + return true; |
| 422 | + } |
| 423 | + } |
| 424 | + return false; |
| 425 | + } |
360 | 426 | } |
361 | 427 |
|
362 | 428 | export function deactivate(): void { |
|
0 commit comments