Skip to content

Commit

Permalink
Add variables for xml.fileAssociations
Browse files Browse the repository at this point in the history
Adds three variables that can be used in `xml.fileAssociations`:
 * ${workspaceFolder}
 * ${fileDirname}
 * ${fileBasenameNoExtension}

These variables can be used for both the `pattern` and the `systemId`.

Closes redhat-developer#307

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Oct 5, 2020
1 parent b44ceb9 commit 5cd3a2f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
22 changes: 22 additions & 0 deletions docs/Validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ Please note that you can use wildcards in the pattern (ex: `foo*.xml`):

In this case, all XML files that start with foo and end with .xml will be associated with the XSD (foo1.xml, foo2.xml, etc)

You can also use the following three variables in either the `pattern` or `systemId`:

| Variable | Meaning |
| --------------------------- | ------------------------------------------------------------------------ |
| ${workspaceFolder} | The absolute path to root folder of the workspace that is currently open |
| ${fileDirname} | The absolute path to the folder of the file that is currently opened |
| ${fileBasenameNoExtension} | The current opened file's basename with no file extension |

If one of the variables for an association can't be expanded (eg. because vscode is opened in rootless mode),
the association is ignored.
This feature is specific to the VSCode client.

## Validation with DTD grammar

To associate your XML with a DTD grammar you can use several strategies:
Expand Down Expand Up @@ -322,7 +334,17 @@ Please note that you can use wildcards in the pattern (ex: `foo*.xml`):

In this case, all XML files that start with foo and end with .xml will be associated with the DTD (foo1.xml, foo2.xml, etc)

You can also use the following three variables in either the `pattern` or `systemId`:

| Variable | Meaning |
| --------------------------- | ------------------------------------------------------------------------ |
| ${workspaceFolder} | The absolute path to root folder of the workspace that is currently open |
| ${fileDirname} | The absolute path to the folder of the file that is currently opened |
| ${fileBasenameNoExtension} | The current opened file's basename with no file extension |

If one of the variables for an association can't be expanded (eg. because vscode is opened in rootless mode),
the association is ignored.
This feature is specific to the VSCode client.

# Other Validation Settings

Expand Down
54 changes: 50 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/

import { prepareExecutable } from './javaServerStarter';
import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams, ReferencesRequest, NotificationType, MessageType } from 'vscode-languageclient';
import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams, ReferencesRequest, NotificationType, MessageType, ShowMessageNotification } from 'vscode-languageclient';
import * as requirements from './requirements';
import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration, Uri, extensions, Command } from "vscode";
import * as path from 'path';
Expand Down Expand Up @@ -249,6 +249,13 @@ export function activate(context: ExtensionContext) {
}));
}

// When the current document changes, update ${fileDirname} and ${fileBasenameNoExtension}
// for the file associations, and send the updated settings to the server
context.subscriptions.push(window.onDidChangeActiveTextEditor(() => {
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getXMLSettings(requirements.java_home) });
onConfigurationChange();
}));

const api: XMLExtensionApi = {
// add API set catalogs to internal memory
addXMLCatalogs: (catalogs: string[]) => {
Expand Down Expand Up @@ -349,11 +356,50 @@ export function activate(context: ExtensionContext) {
xml['xml']['catalogs'].push(catalog);
}
})
externalXmlSettings.xmlFileAssociations.forEach(element => {
if (!xml['xml']['fileAssociations'].some(fileAssociation => fileAssociation.systemId === element.systemId)) {
xml['xml']['fileAssociations'].push(element);
const variableSubstitutedAssociations: XMLFileAssociation[] =
xml['xml']['fileAssociations'].map((association: XMLFileAssociation): XMLFileAssociation => {

const currentFile: string = window.activeTextEditor.document.uri.fsPath;
let currentWorkspace: string = workspace.getWorkspaceFolder(window.activeTextEditor.document.uri).uri.fsPath
|| workspace.workspaceFolders[0].uri.fsPath;

if (!currentWorkspace
&& (association.pattern.indexOf('&{workspaceFolder}') >= 0
|| association.systemId.indexOf('&{workspaceFolder}') >= 0)) {
return;
}

if (!currentFile
&& (association.pattern.indexOf('&{fileDirname}') >= 0
|| association.systemId.indexOf('&{fileDirname}') >= 0
|| association.pattern.indexOf('&{fileBasenameNoExtension}') >= 0
|| association.systemId.indexOf('&{fileBasenameNoExtension}') >= 0)) {
return;
}

/**
* Returns the string with the values for:
* * ${workspaceFolder}
* * ${fileDirname}
* * ${fileBasenameNoExtension}
* substituted into the string
*
* @param val the value to substitute the variables into
* @return the string with values for the variables subtituted into the string
*/
const subVars = (val: string): string => {
let newVal: string = val.replace(/\$\{workspaceFolder\}/g, currentWorkspace);
newVal = newVal.replace(/\$\{fileDirname\}/g, path.dirname(currentFile));
newVal = newVal.replace(/\$\{fileBasenameNoExtension\}/g, path.basename(currentFile, path.extname(currentFile)));
return newVal;
}

return {
pattern: subVars(association.pattern),
systemId: subVars(association.systemId)
};
});
xml['xml']['fileAssociations'] = [...variableSubstitutedAssociations];

return xml;
}
Expand Down

0 comments on commit 5cd3a2f

Please sign in to comment.