Skip to content

Commit 389f6e7

Browse files
author
Alexander Chen
committed
addded grammar association wrapper
1 parent 87e8474 commit 389f6e7

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

src/commands/registerCommands.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -139,49 +139,59 @@ for (const label of bindingTypes.keys()) {
139139
}
140140

141141
/**
142-
* Register commands used for associating grammar file (XSD,DTD) to a given XML file
142+
* The function passed to context subscriptions for grammar association
143143
*
144-
* @param context the extension context
144+
* @param uriString the string representing the XML file path
145+
* @param languageClient the language server client
145146
*/
146-
function registerCodeLensAssociationCommands(context: ExtensionContext, languageClient: LanguageClient) {
147-
context.subscriptions.push(commands.registerCommand(ClientCommandConstants.OPEN_BINDING_WIZARD, async (uriString: string) => {
148-
// A click on Bind to grammar/schema... has been processed in the XML document which is not bound to a grammar
149-
const documentURI = Uri.parse(uriString);
150-
151-
// Step 1 : open a combo to select the binding type ("standard", "xml-model")
152-
const pickedBindingTypeOption = await window.showQuickPick(bindingTypeOptions, { placeHolder: "Binding type" });
153-
if(!pickedBindingTypeOption) {
154-
return;
147+
async function grammarAssociationCommand (uriString: string, languageClient: LanguageClient) {
148+
// A click on Bind to grammar/schema... has been processed in the XML document which is not bound to a grammar
149+
const documentURI = Uri.parse(uriString);
150+
151+
// Step 1 : open a combo to select the binding type ("standard", "xml-model")
152+
const pickedBindingTypeOption = await window.showQuickPick(bindingTypeOptions, { placeHolder: "Binding type" });
153+
if(!pickedBindingTypeOption) {
154+
return;
155+
}
156+
const bindingType = bindingTypes.get(pickedBindingTypeOption.label);
157+
158+
// Open a dialog to select the XSD, DTD to bind.
159+
const options: OpenDialogOptions = {
160+
canSelectMany: false,
161+
openLabel: 'Select XSD or DTD file',
162+
filters: {
163+
'Grammar files': ['xsd', 'dtd']
155164
}
156-
const bindingType = bindingTypes.get(pickedBindingTypeOption.label);
157-
158-
// Open a dialog to select the XSD, DTD to bind.
159-
const options: OpenDialogOptions = {
160-
canSelectMany: false,
161-
openLabel: 'Select XSD or DTD file',
162-
filters: {
163-
'Grammar files': ['xsd', 'dtd']
165+
};
166+
167+
const fileUri = await window.showOpenDialog(options);
168+
if (fileUri && fileUri[0]) {
169+
// The XSD, DTD has been selected, get the proper syntax for binding this grammar file in the XML document.
170+
const identifier = TextDocumentIdentifier.create(documentURI.toString());
171+
const grammarURI = fileUri[0];
172+
try {
173+
const result = await commands.executeCommand(ServerCommandConstants.ASSOCIATE_GRAMMAR_INSERT, identifier, grammarURI.toString(), bindingType);
174+
// Insert the proper syntax for binding
175+
const lspTextDocumentEdit = <TextDocumentEdit>result;
176+
const workEdits = new WorkspaceEdit();
177+
for (const edit of lspTextDocumentEdit.edits) {
178+
workEdits.replace(documentURI, languageClient.protocol2CodeConverter.asRange(edit.range), edit.newText);
164179
}
180+
workspace.applyEdit(workEdits); // apply the edits
181+
} catch (error) {
182+
window.showErrorMessage('Error during grammar binding: ' + error.message);
165183
};
184+
}
185+
}
166186

167-
const fileUri = await window.showOpenDialog(options);
168-
if (fileUri && fileUri[0]) {
169-
// The XSD, DTD has been selected, get the proper syntax for binding this grammar file in the XML document.
170-
const identifier = TextDocumentIdentifier.create(documentURI.toString());
171-
const grammarURI = fileUri[0];
172-
try {
173-
const result = await commands.executeCommand(ServerCommandConstants.ASSOCIATE_GRAMMAR_INSERT, identifier, grammarURI.toString(), bindingType);
174-
// Insert the proper syntax for binding
175-
const lspTextDocumentEdit = <TextDocumentEdit>result;
176-
const workEdits = new WorkspaceEdit();
177-
for (const edit of lspTextDocumentEdit.edits) {
178-
workEdits.replace(documentURI, languageClient.protocol2CodeConverter.asRange(edit.range), edit.newText);
179-
}
180-
workspace.applyEdit(workEdits); // apply the edits
181-
} catch (error) {
182-
window.showErrorMessage('Error during grammar binding: ' + error.message);
183-
};
184-
}
187+
/**
188+
* Register commands used for associating grammar file (XSD,DTD) to a given XML file with CodeLens
189+
*
190+
* @param context the extension context
191+
* @param languageClient the language server client
192+
*/
193+
function registerCodeLensAssociationCommands(context: ExtensionContext, languageClient: LanguageClient) {
194+
context.subscriptions.push(commands.registerCommand(ClientCommandConstants.OPEN_BINDING_WIZARD, async (uriString: string) => {
195+
grammarAssociationCommand(uriString, languageClient)
185196
}));
186-
187-
}
197+
}

0 commit comments

Comments
 (0)