|
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, ConfigurationRequest, ConfigurationParams } from 'vscode-languageclient'; |
15 | | -import * as requirements from './requirements'; |
16 | | -import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration, Uri, extensions, Command, TextEditor } 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, TextEditor, Uri, window, workspace, WorkspaceFolder } from "vscode"; |
| 16 | +import { ConfigurationParams, ConfigurationRequest, DidChangeConfigurationNotification, LanguageClient, LanguageClientOptions, MessageType, NotificationType, ReferencesRequest, RequestType, RevealOutputChannelOn, TextDocumentPositionParams } from 'vscode-languageclient'; |
20 | 17 | import { Commands } from './commands'; |
21 | | -import { getXMLConfiguration, 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"; |
@@ -276,6 +276,15 @@ export function activate(context: ExtensionContext) { |
276 | 276 | } |
277 | 277 | return result; |
278 | 278 | }); |
| 279 | + // When the current document changes, update ${fileDirname} and ${fileBasenameNoExtension} |
| 280 | + // for the file associations, (but only if these variables are referenced), |
| 281 | + // and send the updated settings to the server |
| 282 | + context.subscriptions.push(window.onDidChangeActiveTextEditor(() => { |
| 283 | + if (fileAssocReferencesCurrentFile()) { |
| 284 | + languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getXMLSettings(requirements.java_home) }); |
| 285 | + onConfigurationChange(); |
| 286 | + } |
| 287 | + })); |
279 | 288 |
|
280 | 289 | const api: XMLExtensionApi = { |
281 | 290 | // add API set catalogs to internal memory |
@@ -377,14 +386,70 @@ export function activate(context: ExtensionContext) { |
377 | 386 | xml['xml']['catalogs'].push(catalog); |
378 | 387 | } |
379 | 388 | }) |
380 | | - externalXmlSettings.xmlFileAssociations.forEach(element => { |
381 | | - if (!xml['xml']['fileAssociations'].some(fileAssociation => fileAssociation.systemId === element.systemId)) { |
382 | | - xml['xml']['fileAssociations'].push(element); |
383 | | - } |
384 | | - }); |
| 389 | + const variableSubstitutedAssociations: XMLFileAssociation[] = |
| 390 | + xml['xml']['fileAssociations'].map((association: XMLFileAssociation): XMLFileAssociation => { |
| 391 | + |
| 392 | + const currentFile: TextDocument = window.activeTextEditor.document; |
| 393 | + const currentFileUri: string = currentFile && currentFile.uri.fsPath; |
| 394 | + const currentWorkspace: WorkspaceFolder = workspace.getWorkspaceFolder(currentFile && currentFile.uri); |
| 395 | + const currentWorkspaceUri: string = (currentWorkspace && currentWorkspace.uri.fsPath) |
| 396 | + || (workspace.workspaceFolders && workspace.workspaceFolders[0].uri.fsPath); |
| 397 | + |
| 398 | + if (!currentWorkspaceUri |
| 399 | + && (association.pattern.indexOf('&{workspaceFolder}') >= 0 |
| 400 | + || association.systemId.indexOf('&{workspaceFolder}') >= 0)) { |
| 401 | + return; |
| 402 | + } |
| 403 | + |
| 404 | + if (!currentFileUri |
| 405 | + && (association.pattern.indexOf('&{fileDirname}') >= 0 |
| 406 | + || association.systemId.indexOf('&{fileDirname}') >= 0 |
| 407 | + || association.pattern.indexOf('&{fileBasenameNoExtension}') >= 0 |
| 408 | + || association.systemId.indexOf('&{fileBasenameNoExtension}') >= 0)) { |
| 409 | + return; |
| 410 | + } |
| 411 | + |
| 412 | + /** |
| 413 | + * Returns the string with the values for: |
| 414 | + * * ${workspaceFolder} |
| 415 | + * * ${fileDirname} |
| 416 | + * * ${fileBasenameNoExtension} |
| 417 | + * substituted into the string |
| 418 | + * |
| 419 | + * @param val the value to substitute the variables into |
| 420 | + * @return the string with values for the variables substituted into the string |
| 421 | + */ |
| 422 | + const subVars = (val: string): string => { |
| 423 | + let newVal: string = val.replace(/\$\{workspaceFolder\}/g, currentWorkspaceUri); |
| 424 | + newVal = newVal.replace(/\$\{fileDirname\}/g, path.dirname(currentFileUri)); |
| 425 | + newVal = newVal.replace(/\$\{fileBasenameNoExtension\}/g, path.basename(currentFileUri, path.extname(currentFileUri))); |
| 426 | + return newVal; |
| 427 | + } |
| 428 | + |
| 429 | + return { |
| 430 | + pattern: subVars(association.pattern), |
| 431 | + systemId: subVars(association.systemId) |
| 432 | + }; |
| 433 | + }); |
| 434 | + xml['xml']['fileAssociations'] = [...variableSubstitutedAssociations]; |
385 | 435 |
|
386 | 436 | return xml; |
387 | 437 | } |
| 438 | + |
| 439 | + /** |
| 440 | + * Returns true if the the XML settings contain a file association with a variable reference to either ${fileDirname} or ${fileBasenameNoExtension} and false otherwise |
| 441 | + * |
| 442 | + * @return true if the the XML settings contain a file association with a variable reference to either ${fileDirname} or ${fileBasenameNoExtension} and false otherwise |
| 443 | + */ |
| 444 | + function fileAssocReferencesCurrentFile(): boolean { |
| 445 | + for (const assoc of getXMLConfiguration().get('fileAssociations') as XMLFileAssociation[]) { |
| 446 | + if (assoc.pattern.indexOf('${fileDirname}') >= 0 || assoc.pattern.indexOf('${fileBasenameNoExtension}') >= 0 |
| 447 | + || assoc.systemId.indexOf('${fileDirname}') >= 0 || assoc.systemId.indexOf('${fileBasenameNoExtension}') >= 0) { |
| 448 | + return true; |
| 449 | + } |
| 450 | + } |
| 451 | + return false; |
| 452 | + } |
388 | 453 | } |
389 | 454 |
|
390 | 455 | export function deactivate(): void { |
|
0 commit comments