|
| 1 | +import * as path from "path"; |
| 2 | +import { TextDocument, window, workspace, WorkspaceFolder } from "vscode"; |
| 3 | +import { XMLFileAssociation } from "./extension"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Represents a variable that refers to a value that can be resolved |
| 7 | + */ |
| 8 | +class VariableSubstitution { |
| 9 | + |
| 10 | + private varName: string; |
| 11 | + private varKind: VariableSubstitutionKind; |
| 12 | + private getValue: (currentFileUri: string, currentWorkspaceUri: string) => string; |
| 13 | + private replaceRegExp: RegExp | undefined; |
| 14 | + |
| 15 | + /** |
| 16 | + * |
| 17 | + * @param name the name of this variable |
| 18 | + * @param kind the kind of this variable |
| 19 | + * @param getValue a function that resolves the value of the variable |
| 20 | + */ |
| 21 | + constructor(name: string, kind: VariableSubstitutionKind, getValue: (currentFileUri: string, currentWorkspaceUri: string) => string) { |
| 22 | + this.varName = name; |
| 23 | + this.varKind = kind; |
| 24 | + this.getValue = getValue; |
| 25 | + } |
| 26 | + |
| 27 | + public get name(): string { |
| 28 | + return this.varName; |
| 29 | + } |
| 30 | + |
| 31 | + public get kind(): VariableSubstitutionKind { |
| 32 | + return this.varKind; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Returns the string with the references to this variable replaced with the value, or the original string if the value cannot be resolved |
| 37 | + * |
| 38 | + * @param original the string to substitute variable value |
| 39 | + * @param currentFileUri the uri of the currently focused file, as a string |
| 40 | + * @param currentWorkspaceUri the uri of the root of the currently open workspace, as a string |
| 41 | + * @returns the string with the references to this variable replaced with the value, or the original string if the value cannot be resolved |
| 42 | + */ |
| 43 | + public substituteString(original: string, currentFileUri: string, currentWorkspaceUri: string): string { |
| 44 | + const value: string = this.getValue(currentFileUri, currentWorkspaceUri); |
| 45 | + return value ? original.replace(this.getReplaceRegExp(), value) : original; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns a regex that matches all references to the variable |
| 50 | + * |
| 51 | + * Lazily initialized |
| 52 | + * |
| 53 | + * @returns a regex that matches all references to the variable |
| 54 | + */ |
| 55 | + public getReplaceRegExp(): RegExp { |
| 56 | + if (!this.replaceRegExp) { |
| 57 | + this.replaceRegExp = new RegExp('\\$\\{' + `${this.name}` + '\\}', 'g'); |
| 58 | + } |
| 59 | + return this.replaceRegExp; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +enum VariableSubstitutionKind { |
| 64 | + Workspace, |
| 65 | + File |
| 66 | +} |
| 67 | + |
| 68 | +const SETTINGS_VARIABLES: VariableSubstitution[] = [ |
| 69 | + new VariableSubstitution( |
| 70 | + "workspaceFolder", |
| 71 | + VariableSubstitutionKind.Workspace, |
| 72 | + (currentFileUri: string, currentWorkspaceUri: string): string => { |
| 73 | + return currentWorkspaceUri; |
| 74 | + } |
| 75 | + ), |
| 76 | + new VariableSubstitution( |
| 77 | + "fileDirname", |
| 78 | + VariableSubstitutionKind.File, |
| 79 | + (currentFileUri: string, currentWorkspaceUri: string): string => { |
| 80 | + return path.dirname(currentFileUri); |
| 81 | + } |
| 82 | + ), |
| 83 | + new VariableSubstitution( |
| 84 | + "fileBasenameNoExtension", |
| 85 | + VariableSubstitutionKind.File, |
| 86 | + (currentFileUri: string, currentWorkspaceUri: string): string => { |
| 87 | + return path.basename(currentFileUri, path.extname(currentFileUri)); |
| 88 | + } |
| 89 | + ) |
| 90 | +]; |
| 91 | + |
| 92 | +/** |
| 93 | + * Returns the file associations with as many variable references resolved as possible |
| 94 | + * |
| 95 | + * @param associations the file associations to resolve the variable references in |
| 96 | + * @returns the file associations with as many variable references resolved as possible |
| 97 | + */ |
| 98 | +export function getVariableSubstitutedAssociations(associations: XMLFileAssociation[]): XMLFileAssociation[] { |
| 99 | + |
| 100 | + // Collect properties needed to resolve variables |
| 101 | + const currentFile: TextDocument = window.activeTextEditor.document; |
| 102 | + const currentFileUri: string = currentFile && currentFile.uri.fsPath; |
| 103 | + const currentWorkspace: WorkspaceFolder = workspace.getWorkspaceFolder(currentFile && currentFile.uri); |
| 104 | + const currentWorkspaceUri: string = (currentWorkspace && currentWorkspace.uri.fsPath) |
| 105 | + || (workspace.workspaceFolders && workspace.workspaceFolders[0].uri.fsPath); |
| 106 | + |
| 107 | + // Remove variables that can't be resolved |
| 108 | + let variablesToSubstitute = SETTINGS_VARIABLES; |
| 109 | + if (!currentWorkspaceUri) { |
| 110 | + variablesToSubstitute = variablesToSubstitute.filter(variable => { variable.kind !== VariableSubstitutionKind.Workspace }); |
| 111 | + } |
| 112 | + if (!currentFileUri) { |
| 113 | + variablesToSubstitute = variablesToSubstitute.filter(variable => { variable.kind !== VariableSubstitutionKind.File }); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns the string with the values for all the variables that can be resolved substituted in the string |
| 118 | + * |
| 119 | + * @param val the value to substitute the variables into |
| 120 | + * @return the string with the values for all the variables that can be resolved substituted in the string |
| 121 | + */ |
| 122 | + const subVars = (val: string): string => { |
| 123 | + let newVal = val; |
| 124 | + for (const settingVariable of variablesToSubstitute) { |
| 125 | + newVal = settingVariable.substituteString(newVal, currentFileUri, currentWorkspaceUri); |
| 126 | + } |
| 127 | + return newVal; |
| 128 | + } |
| 129 | + |
| 130 | + return associations.map((association: XMLFileAssociation) => { |
| 131 | + return { |
| 132 | + pattern: subVars(association.pattern), |
| 133 | + systemId: subVars(association.systemId) |
| 134 | + }; |
| 135 | + }); |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * Returns true if any of the file associations contain references to variables that refer to current file, and false otherwise |
| 140 | + * |
| 141 | + * @param associations A list of file associations to check |
| 142 | + * @returns true if any of the file associations contain references to variables that refer to current file, and false otherwise |
| 143 | + */ |
| 144 | +export function containsVariableReferenceToCurrentFile(associations: XMLFileAssociation[]): boolean { |
| 145 | + const fileVariables: VariableSubstitution[] = SETTINGS_VARIABLES.filter(variable => variable.kind === VariableSubstitutionKind.File); |
| 146 | + for (const association of associations) { |
| 147 | + for (const variable of fileVariables) { |
| 148 | + if (variable.getReplaceRegExp().test(association.pattern) || variable.getReplaceRegExp().test(association.systemId)) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + return false; |
| 154 | +} |
0 commit comments