Skip to content

Commit 40859e1

Browse files
autoClosingBrackets are forced off if autoCloseTags is enabled
Fixes #38 Signed-off-by: Nikolas Komonen <nikolaskomonen@gmail.com>
1 parent 022d05b commit 40859e1

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,15 @@
146146
"xml.completion.autoCloseTags": {
147147
"type": "boolean",
148148
"default": true,
149-
"description": "Enable/disable ability to autoclose tags. \n\n**IMPORTANT: Turn off editor.autoClosingTags for this to work\n**",
149+
"description": "Enable/disable autoclosing of XML tags. \n\nIMPORTANT: Turn off editor.autoClosingTags for this to work",
150150
"scope": "window"
151151
}
152152
}
153+
},
154+
"configurationDefaults": {
155+
"[xml]": {
156+
"editor.autoClosingBrackets": "never"
157+
}
153158
}
154159
}
155160
}

src/extension.ts

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@
1313
import { prepareExecutable } from './javaServerStarter';
1414
import { LanguageClientOptions, RevealOutputChannelOn, LanguageClient, DidChangeConfigurationNotification, RequestType, TextDocumentPositionParams } from 'vscode-languageclient';
1515
import * as requirements from './requirements';
16-
import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, WorkspaceConfiguration, LanguageConfiguration } from "vscode";
16+
import { languages, IndentAction, workspace, window, commands, ExtensionContext, TextDocument, Position, LanguageConfiguration } from "vscode";
1717
import * as path from 'path';
1818
import * as os from 'os';
1919
import { activateTagClosing } from './tagClosing';
20+
import { WorkspaceFoldersFeature } from 'vscode-languageclient/lib/workspaceFolders';
21+
22+
export interface ScopeInfo {
23+
scope : "default" | "global" | "workspace" | "folder";
24+
configurationTarget: boolean;
25+
}
2026

2127
namespace TagCloseRequest {
2228
export const type: RequestType<TextDocumentPositionParams, string, any, any> = new RequestType('xml/closeTag');
2329
}
2430

31+
let ignoreAutoCloseTags = false;
32+
2533
export function activate(context: ExtensionContext) {
2634
let storagePath = context.storagePath;
2735
if (!storagePath) {
@@ -45,11 +53,18 @@ export function activate(context: ExtensionContext) {
4553
revealOutputChannelOn: RevealOutputChannelOn.Never,
4654
initializationOptions: { settings: getSettings() },
4755
synchronize: {
48-
configurationSection: ['xml']
56+
//preferences starting with these will trigger didChangeConfiguration
57+
configurationSection: ['xml', '[xml]']
4958
},
5059
middleware: {
5160
workspace: {
52-
didChangeConfiguration: () => languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getSettings() })
61+
didChangeConfiguration: () => {
62+
languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getSettings() });
63+
if(!ignoreAutoCloseTags) {
64+
verifyAutoClosing();
65+
}
66+
}
67+
5368
}
5469
}
5570
}
@@ -86,7 +101,7 @@ export function activate(context: ExtensionContext) {
86101
client: true
87102
},
88103
format: {
89-
enabled : true,
104+
enabled: true,
90105
splitAttributes: false
91106
},
92107
completion: {
@@ -102,11 +117,62 @@ export function activate(context: ExtensionContext) {
102117
settings['logs']['file'] = logfile;
103118
return settings;
104119
}
120+
}
105121

106122

123+
function verifyAutoClosing() {
124+
let configXML = workspace.getConfiguration();
125+
let closeTags = configXML.get("xml.completion.autoCloseTags");
126+
let closeBrackets = configXML.get("[xml]")["editor.autoClosingBrackets"];
127+
if (closeTags && closeBrackets != "never") {
128+
window.showWarningMessage(
129+
"The [xml].editor.autoClosingBrackets setting conflicts with xml.completion.autoCloseTags. It's recommended to disable it.",
130+
"Disable",
131+
"Ignore").then((selection) => {
132+
if (selection == "Disable") {
133+
let scopeInfo : ScopeInfo = getScopeLevel("", "[xml]");
134+
workspace.getConfiguration().update("[xml]", { "editor.autoClosingBrackets": "never" }, scopeInfo.configurationTarget).then(
135+
() => console.log('[xml].editor.autoClosingBrackets globally set to never'),
136+
(error) => console.log(error)
137+
);
138+
}
139+
else if(selection == "Ignore") {
140+
ignoreAutoCloseTags = true;
141+
}
142+
});
143+
}
144+
}
107145

146+
function getScopeLevel(configurationKey : string, key : string) : ScopeInfo{
108147

148+
let configXML = workspace.getConfiguration(configurationKey);
149+
let result = configXML.inspect(key);
150+
let scope, configurationTarget;
151+
if(result.workspaceFolderValue == undefined) {
152+
if(result.workspaceValue == undefined) {
153+
if(result.globalValue == undefined) {
154+
scope = "default"
155+
configurationTarget = true;
156+
}
157+
else {
158+
scope = "global";
159+
configurationTarget = true;
160+
}
161+
}
162+
else {
163+
scope = "workspace";
164+
configurationTarget = false;
165+
}
166+
}
167+
else {
168+
scope = "folder";
169+
configurationTarget = undefined;
170+
}
171+
let scopeInfo : ScopeInfo = {"scope": scope, "configurationTarget": configurationTarget};
172+
return scopeInfo;
173+
109174
}
175+
110176
function getIndentationRules(): LanguageConfiguration {
111177
return {
112178
onEnterRules: [

0 commit comments

Comments
 (0)