Skip to content

Commit 447d2ab

Browse files
JessicaJHeeangelozerr
authored andcommitted
# This is a combination of 2 commits.
Add codelens and command to register/unregister catalog Signed-off-by: Jessica He <jhe@redhat.com>
1 parent a833f7d commit 447d2ab

7 files changed

Lines changed: 105 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ This VS Code extension provides support for creating and editing XML documents,
4040
* XSD based code completion
4141
* XSL support
4242
* [XInclude support](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Features/XIncludeFeatures.md#Validation)
43-
* [XML catalogs](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Validation.md#xml-catalog-with-xsd)
43+
* [XML catalogs](https://github.com/redhat-developer/vscode-xml/blob/main/docs/Features/XMLCatalogFeatures.md#xml-catalog-features)
4444
* File associations
4545
* Code actions
4646
* Schema Caching

docs/Features.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
- [DTD features](Features/DTDFeatures.md#dtd-features)
88
- [RelaxNG features](Features/RelaxNGFeatures.md#relaxng-features)
99
- [XInclude features](Features/XIncludeFeatures.md#xinclude-features)
10+
- [XML Catalog features](Features/XMLCatalogFeatures.md#xml-catalog-features)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# XML Catalog Features
2+
3+
## Validation
4+
5+
XML Catalog is supported with both DTD and XSD. Please see here for more information on the usage of [XML Catalog With DTD](../Validation.md#xml-catalog-with-dtd) and [XML Catalog With XSD](../Validation.md#xml-catalog-with-xsd).
6+
7+
## Code Lens
8+
9+
CodeLens is supported for XML catalogs, which shows whether the catalog is registered or not.
10+
It also provides the ability to register or unregister the catalog in settings.json.
11+
12+
![CodeLens Catalog Registration](../images/Features/CodeLensCatalogRegistration.gif)
213 KB
Loading
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* LSP Command Constants.
3+
*/
4+
5+
/**
6+
* Command to register/unregister catalog.
7+
*/
8+
export const UPDATE_CONFIGURATION = 'xml.update.configuration'
9+

src/commands/registerCommands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as vscode from 'vscode';
33
import { commands, ConfigurationTarget, env, ExtensionContext, OpenDialogOptions, Position, QuickPickItem, SnippetString, TextDocument, Uri, window, workspace, WorkspaceEdit, Selection } from "vscode";
44
import { CancellationToken, ExecuteCommandParams, ExecuteCommandRequest, ReferencesRequest, TextDocumentEdit, TextDocumentIdentifier, TextEdit } from "vscode-languageclient";
55
import { LanguageClient } from 'vscode-languageclient/node';
6+
import { registerConfigurationUpdateCommand } from '../lsp-commands';
67
import { markdownPreviewProvider } from "../markdownPreviewProvider";
78
import { DEBUG } from '../server/java/javaServerStarter';
89
import { getDirectoryPath, getFileName, getRelativePath, getWorkspaceUri } from '../utils/fileUtils';
@@ -33,6 +34,7 @@ export async function registerClientServerCommands(context: ExtensionContext, la
3334
registerRefactorCommands(context, languageClient);
3435
registerAssociationCommands(context, languageClient);
3536
registerRestartLanguageServerCommand(context, languageClient);
37+
registerConfigurationUpdateCommand();
3638

3739
// Register client command to execute custom XML Language Server command
3840
context.subscriptions.push(commands.registerCommand(ClientCommandConstants.EXECUTE_WORKSPACE_COMMAND, (command, ...rest) => {

src/lsp-commands.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright 2022 Red Hat, Inc. and others.
3+
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { commands, ConfigurationTarget, Disposable, workspace, window } from 'vscode';
18+
import { getDirectoryPath, getRelativePath, getWorkspaceUri } from './utils/fileUtils';
19+
import * as CommandKind from './commands/lspCommandConstants';
20+
21+
/**
22+
* Registers the `CommandKind.UPDATE_CONFIGURATION` command
23+
*/
24+
export function registerConfigurationUpdateCommand(): Disposable {
25+
return commands.registerCommand(CommandKind.UPDATE_CONFIGURATION, resolveConfigurationItemEdit);
26+
}
27+
28+
function resolveConfigurationItemEdit<T>(configurationItemEdit: ConfigurationItemEdit) {
29+
if (configurationItemEdit.valueKind == ConfigurationItemValueKind.file) {
30+
configurationItemEdit.value = resolveFilePath(configurationItemEdit.value);
31+
}
32+
switch (configurationItemEdit.editType) {
33+
case ConfigurationItemEditType.Add:
34+
addToPreferenceArray<T>(configurationItemEdit.section, configurationItemEdit.value);
35+
break;
36+
case ConfigurationItemEditType.Delete: {
37+
removeFromPreferenceArray<T>(configurationItemEdit.section, configurationItemEdit.value);
38+
}
39+
}
40+
}
41+
42+
function resolveFilePath(filePath: any): any {
43+
const currentWorkspaceUri = getWorkspaceUri(window.activeTextEditor.document).toString();
44+
return getDirectoryPath(filePath).includes(currentWorkspaceUri) ? getRelativePath(currentWorkspaceUri, filePath) : filePath;
45+
46+
}
47+
48+
function addToPreferenceArray<T>(key: string, value: T): void {
49+
const configArray: T[] = workspace.getConfiguration().get<T[]>(key, []);
50+
if (configArray.includes(value)) {
51+
return;
52+
}
53+
configArray.push(value);
54+
workspace.getConfiguration().update(key, configArray, ConfigurationTarget.Workspace);
55+
}
56+
57+
function removeFromPreferenceArray<T>(key: string, value: T): void {
58+
const configArray: T[] = workspace.getConfiguration().get<T[]>(key, []);
59+
if (!configArray.includes(value)) {
60+
return;
61+
}
62+
const resultPref = configArray.filter(i => i != value);
63+
workspace.getConfiguration().update(key, resultPref, ConfigurationTarget.Workspace);
64+
}
65+
66+
interface ConfigurationItemEdit {
67+
section: string;
68+
value: any;
69+
editType: ConfigurationItemEditType;
70+
valueKind: ConfigurationItemValueKind;
71+
}
72+
73+
enum ConfigurationItemEditType {
74+
Add = 0,
75+
Delete = 1
76+
}
77+
78+
enum ConfigurationItemValueKind {
79+
file = 0
80+
}

0 commit comments

Comments
 (0)