Skip to content

Commit a033cee

Browse files
Alexander Chenfbricon
authored andcommitted
Added qute.templates.languageMismatch to avoid reuse of quarkus.tools.propertiesLanguageMismatch
Signed-off-by: Alexander Chen <alchen@redhat.com>
1 parent 4446927 commit a033cee

File tree

6 files changed

+176
-94
lines changed

6 files changed

+176
-94
lines changed

package.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,34 @@
206206
"type": "string",
207207
"enum": [
208208
"ignore",
209-
"forceQuarkus",
209+
"force",
210210
"prompt"
211211
],
212212
"enumDescriptions": [
213-
"ignore language mismatch for Quarkus properties",
213+
"Ignore language mismatch for Quarkus properties",
214214
"Automatically correct the language for mismatched Quarkus properties",
215215
"Prompt correcting the language for mismatched Quarkus properties"
216216
],
217-
"default": "forceQuarkus",
217+
"default": "force",
218218
"description": "Action performed when detected Quarkus properties have an incorrect language.",
219219
"scope": "window"
220220
},
221+
"qute.templates.languageMismatch": {
222+
"type": "string",
223+
"enum": [
224+
"ignore",
225+
"force",
226+
"prompt"
227+
],
228+
"enumDescriptions": [
229+
"Ignore language mismatch for Qute templates",
230+
"Automatically correct the language for mismatched Qute templates",
231+
"Prompt correcting the language for mismatched Qute templates"
232+
],
233+
"default": "force",
234+
"description": "Action performed when detected Qute templates have an incorrect language.",
235+
"scope": "window"
236+
},
221237
"qute.trace.server": {
222238
"type": "string",
223239
"enum": [

src/QuarkusConfig.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
DEFAULT_API_URL,
1818
} from './definitions/constants';
1919
import { ConfigurationTarget, workspace } from 'vscode';
20+
import { LanguageMismatch } from './utils/languageMismatch';
2021

2122
/**
2223
* This class manages the extension's interaction with
@@ -30,7 +31,7 @@ export namespace QuarkusConfig {
3031
export const STARTER_SHOW_EXT_DESC = 'quarkus.tools.starter.showExtensionDescriptions';
3132
export const ALWAYS_SHOW_WELCOME_PAGE = 'quarkus.tools.alwaysShowWelcomePage';
3233
export const DEBUG_TERMINATE_ON_EXIT = 'quarkus.tools.debug.terminateProcessOnExit';
33-
export const PROPERTIES_LANGUAGE_MISMATCH = "quarkus.tools.propertiesLanguageMismatch";
34+
export const QUARKUS_PROPERTIES_LANGUAGE_MISMATCH = "quarkus.tools.propertiesLanguageMismatch";
3435
export const QUARKUS_OVERRIDE_LANGUAGE_ID = 'quarkus.tools.override.languageId';
3536

3637
export function getApiUrl(): string {
@@ -52,8 +53,8 @@ export namespace QuarkusConfig {
5253
return workspace.getConfiguration().get<TerminateProcessConfig>(DEBUG_TERMINATE_ON_EXIT);
5354
}
5455

55-
export function getPropertiesLanguageMismatch(): PropertiesLanguageMismatch {
56-
return workspace.getConfiguration().get<PropertiesLanguageMismatch>(PROPERTIES_LANGUAGE_MISMATCH);
56+
export function getPropertiesLanguageMismatch(): QuarkusPropertiesLanguageMismatch {
57+
return workspace.getConfiguration().get<QuarkusPropertiesLanguageMismatch>(QUARKUS_PROPERTIES_LANGUAGE_MISMATCH);
5758
}
5859

5960
export function setAlwaysShowWelcomePage(value: boolean): void {
@@ -68,8 +69,8 @@ export namespace QuarkusConfig {
6869
saveToQuarkusConfig(DEBUG_TERMINATE_ON_EXIT, value);
6970
}
7071

71-
export function setPropertiesLanguageMismatch(value: PropertiesLanguageMismatch): Thenable<void> {
72-
return saveToQuarkusConfig(PROPERTIES_LANGUAGE_MISMATCH, value);
72+
export function setPropertiesLanguageMismatch(value: QuarkusPropertiesLanguageMismatch): Thenable<void> {
73+
return saveToQuarkusConfig(QUARKUS_PROPERTIES_LANGUAGE_MISMATCH, value);
7374
}
7475

7576
export function saveToQuarkusConfig<T>(configName: string, value: T): Thenable<void> {
@@ -83,8 +84,4 @@ export enum TerminateProcessConfig {
8384
DontTerminate = "Never terminate"
8485
}
8586

86-
export enum PropertiesLanguageMismatch {
87-
ignore = "ignore",
88-
forceQuarkus = "forceQuarkus",
89-
prompt = "prompt"
90-
}
87+
export class QuarkusPropertiesLanguageMismatch extends LanguageMismatch {}

src/extension.ts

Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import * as path from 'path';
1717
import { commands, ConfigurationChangeEvent, Disposable, ExtensionContext, extensions, languages, Terminal, TextDocument, window, workspace } from 'vscode';
1818
import { registerVSCodeCommands } from './commands/registerCommands';
1919
import { ProjectLabelInfo } from './definitions/ProjectLabelInfo';
20-
import { PropertiesLanguageMismatch, QuarkusConfig } from './QuarkusConfig';
20+
import { QuarkusPropertiesLanguageMismatch, QuarkusConfig } from './QuarkusConfig';
2121
import { QuarkusContext } from './QuarkusContext';
2222
import quarkusProjectListener from './QuarkusProjectListener';
2323
import { connectToQuteLS } from './qute/languageServer/client';
2424
import { terminalCommandRunner } from './terminal/terminalCommandRunner';
25+
import { tryToForceLanguageId } from './utils/languageMismatch';
2526
import { JAVA_EXTENSION_ID } from './utils/requestStandardMode';
2627
import { initTelemetryService } from './utils/telemetryUtils';
2728
import { WelcomeWebview } from './webviews/WelcomeWebview';
@@ -51,13 +52,13 @@ export async function activate(context: ExtensionContext) {
5152

5253
// When extension is started, loop for each text documents which are opened to update their language ID.
5354
workspace.textDocuments.forEach(document => {
54-
updateLanguageId(document, false);
55+
updateLanguageId(context, document, false);
5556
});
5657
// When a text document is opened, update their language ID.
5758
context.subscriptions.push(
5859
workspace.onDidOpenTextDocument(async (document) => {
5960
if (!(document.uri.scheme === 'git')) {
60-
await updateLanguageId(document, true);
61+
await updateLanguageId(context, document, true);
6162
}
6263
})
6364
);
@@ -84,80 +85,18 @@ function displayWelcomePageIfNeeded(context: ExtensionContext): void {
8485
}
8586
}
8687

87-
/**
88-
* Try to force the document language ID to the given language ID.
89-
*
90-
* @param document the text document.
91-
* @param languageId the language ID.
92-
* @param notifyUser if a notification should appear when the language ID is updated
93-
*/
94-
function tryToForceLanguageId(document: TextDocument, fileName: string, propertiesLanguageMismatch: PropertiesLanguageMismatch, languageId: string, notifyUser: boolean): Promise<void> {
95-
// Get project label information for the given file URI
96-
const labelInfo = ProjectLabelInfo.getProjectLabelInfo(document.uri.toString());
97-
return labelInfo.then(l => {
98-
if (l.isQuarkusProject()) {
99-
if (propertiesLanguageMismatch === PropertiesLanguageMismatch.prompt) {
100-
const YES = "Yes", NO = "No";
101-
const supportType = languageId.indexOf('qute') > -1 ? 'Qute' : 'Quarkus';
102-
const response: Thenable<string> = window.showInformationMessage(`The language ID for '${fileName}' must be '${languageId}' to receive ${supportType} support. Set the language ID to '${languageId}?'`, YES, NO);
103-
response.then(result => {
104-
if (result === YES) {
105-
// The application.properties file belong to a Quarkus project, force to the quarkus-properties language
106-
languages.setTextDocumentLanguage(document, languageId);
107-
}
108-
});
109-
} else {
110-
// The application.properties file belong to a Quarkus project, force to the quarkus-properties language
111-
const oldLanguageId: string = document.languageId;
112-
languages.setTextDocumentLanguage(document, languageId);
113-
if (notifyUser) {
114-
const context = QuarkusContext.getExtensionContext();
115-
if (!hasShownSetLanguagePopUp(context)) {
116-
const CONFIGURE_IN_SETTINGS = "Configure in Settings";
117-
const DISABLE_IN_SETTINGS = "Disable Language Updating";
118-
const response: Thenable<string> = window.showInformationMessage(
119-
`Quarkus Tools for Visual Studio Code automatically switched the language ID of '${fileName}' `
120-
+ `to be '${languageId}' in order to provide language support. `
121-
+ `This behavior can be configured in settings.`, DISABLE_IN_SETTINGS, CONFIGURE_IN_SETTINGS);
122-
response.then(result => {
123-
if (result === CONFIGURE_IN_SETTINGS) {
124-
commands.executeCommand('workbench.action.openSettings', QuarkusConfig.PROPERTIES_LANGUAGE_MISMATCH);
125-
} else if (result === DISABLE_IN_SETTINGS) {
126-
QuarkusConfig.setPropertiesLanguageMismatch(PropertiesLanguageMismatch.ignore).then(() => {
127-
languages.setTextDocumentLanguage(document, oldLanguageId);
128-
});
129-
}
130-
});
131-
context.globalState.update(QuarkusConfig.QUARKUS_OVERRIDE_LANGUAGE_ID, 'true');
132-
}
133-
}
134-
}
135-
}
136-
});
137-
}
138-
139-
function hasShownSetLanguagePopUp(context: ExtensionContext): boolean {
140-
return context.globalState.get(QuarkusConfig.QUARKUS_OVERRIDE_LANGUAGE_ID, false);
141-
}
142-
14388
const APP_PROPERTIES_PATTERN = /^application(?:-[A-Za-z]+)\.properties$/;
14489

145-
const LANGUAGE_MAP = new Map<string, string>([
146-
[".html", "qute-html"],
147-
[".txt", "qute-txt"],
148-
[".yaml", "qute-yaml"],
149-
[".json", "qute-json"]
150-
]);
151-
15290
/**
15391
* Update if required the language ID to 'quarkus-properties' if needed.
15492
*
93+
* @param context the extension context
15594
* @param document the text document.
15695
* @param onExtensionLoad if the user manually changed the language id.
15796
*/
158-
async function updateLanguageId(document: TextDocument, onExtensionLoad: boolean) {
159-
const propertiesLanguageMismatch: PropertiesLanguageMismatch = QuarkusConfig.getPropertiesLanguageMismatch();
160-
if (propertiesLanguageMismatch === PropertiesLanguageMismatch.ignore) {
97+
async function updateLanguageId(context: ExtensionContext, document: TextDocument, onExtensionLoad: boolean) {
98+
const propertiesLanguageMismatch: QuarkusPropertiesLanguageMismatch = QuarkusConfig.getPropertiesLanguageMismatch();
99+
if (propertiesLanguageMismatch === QuarkusPropertiesLanguageMismatch.ignore) {
161100
// Do nothing
162101
return;
163102
}
@@ -167,21 +106,13 @@ async function updateLanguageId(document: TextDocument, onExtensionLoad: boolean
167106
// the language ID is already quarkus-properties do nothing.
168107
return;
169108
}
170-
tryToForceLanguageId(document, fileName, propertiesLanguageMismatch, 'quarkus-properties', onExtensionLoad);
109+
tryToForceLanguageId(context, document, fileName, propertiesLanguageMismatch, 'quarkus-properties', onExtensionLoad, QuarkusConfig.QUARKUS_OVERRIDE_LANGUAGE_ID, QuarkusConfig.QUARKUS_PROPERTIES_LANGUAGE_MISMATCH);
171110
} else if (fileName === 'application.yaml' || fileName === 'application.yml') {
172111
if (document.languageId === 'yaml') {
173112
// the language ID is already yaml do nothing.
174113
return;
175114
}
176-
tryToForceLanguageId(document, fileName, propertiesLanguageMismatch, 'yaml', onExtensionLoad);
177-
} else if (document.fileName.includes(`resources${path.sep}templates${path.sep}`)) {
178-
for (const extension of LANGUAGE_MAP.keys()) {
179-
if (path.extname(document.fileName) === extension) {
180-
const quteLanguageId = LANGUAGE_MAP.get(extension);
181-
tryToForceLanguageId(document, fileName, propertiesLanguageMismatch, quteLanguageId, onExtensionLoad);
182-
break;
183-
}
184-
}
115+
tryToForceLanguageId(context, document, fileName, propertiesLanguageMismatch, 'yaml', onExtensionLoad, QuarkusConfig.QUARKUS_OVERRIDE_LANGUAGE_ID, QuarkusConfig.QUARKUS_PROPERTIES_LANGUAGE_MISMATCH);
185116
}
186117
}
187118

src/qute/commands/registerCommands.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import * as path from 'path';
12
import { TextEncoder } from "util";
2-
import { commands, ConfigurationTarget, ExtensionContext, Position, Range, Selection, TextDocument, TextEditor, Uri, window, workspace, WorkspaceConfiguration } from "vscode";
3+
import { commands, ConfigurationTarget, ExtensionContext, Position, Range, Selection, TextDocument, TextEditor, Uri, window, workspace, WorkspaceConfiguration, languages } from "vscode";
34
import { ConfigurationItem, Location } from "vscode-languageclient";
4-
import { QuteSettings } from "../languageServer/settings";
5+
import { QuteSettings, QuteTemplateLanguageMismatch } from "../languageServer/settings";
56
import { QuteClientCommandConstants, QuteJdtLsServerCommandConstants, QuteServerCommandConstants } from "./commandConstants";
67
import { CancellationToken, ExecuteCommandParams, ExecuteCommandRequest } from "vscode-languageclient";
78
import { LanguageClient } from "vscode-languageclient/node";
9+
import { tryToForceLanguageId } from '../../utils/languageMismatch';
810

911
/**
1012
* Register custom vscode command for Qute support.
@@ -17,6 +19,17 @@ export function registerVSCodeQuteCommands(context: ExtensionContext) {
1719
registerJavaDefinitionCommand(context);
1820
registerConfigurationUpdateCommand(context);
1921
registerQuteValidationToggleCommand(context);
22+
context.subscriptions.push(
23+
workspace.onDidOpenTextDocument((document) => {
24+
if (!(document.uri.scheme === 'git')) {
25+
updateQuteLanguageId(context, document, true);
26+
}
27+
})
28+
);
29+
// When extension is started, loop for each text documents which are opened to update their language ID.
30+
workspace.textDocuments.forEach(document => {
31+
updateQuteLanguageId(context, document, false);
32+
});
2033
}
2134

2235
export function registerQuteExecuteWorkspaceCommand(context: ExtensionContext, languageClient: LanguageClient) {
@@ -214,6 +227,42 @@ function getSettingsValue(value: any, section: string, editType: ConfigurationIt
214227
return value;
215228
}
216229

230+
const LANGUAGE_MAP = new Map<string, string>([
231+
[".html", "qute-html"],
232+
[".htm", "qute-html"],
233+
[".txt", "qute-txt"],
234+
[".yaml", "qute-yaml"],
235+
[".yml", "qute-yaml"],
236+
[".json", "qute-json"]
237+
]);
238+
239+
/**
240+
* Update the language ID to a supported Qute language id if needed.
241+
*
242+
* @param context the extension context
243+
* @param document the text document.
244+
* @param onExtensionLoad if the user manually changed the language id.
245+
*/
246+
async function updateQuteLanguageId(context: ExtensionContext, document: TextDocument, onExtensionLoad: boolean) {
247+
const propertiesLanguageMismatch: QuteTemplateLanguageMismatch = QuteSettings.getQuteTemplatesLanguageMismatch();
248+
// Check if the setting is set to ignore or if the language ID is already set to Qute
249+
if (propertiesLanguageMismatch === QuteTemplateLanguageMismatch.ignore || document.languageId.startsWith('qute-')) {
250+
// Do nothing
251+
return;
252+
}
253+
const fileName: string = path.basename(document.fileName);
254+
if (document.fileName.includes(`resources${path.sep}templates${path.sep}`)) {
255+
for (const extension of LANGUAGE_MAP.keys()) {
256+
if (path.extname(document.fileName) === extension) {
257+
const quteLanguageId = LANGUAGE_MAP.get(extension);
258+
259+
tryToForceLanguageId(context, document, fileName, propertiesLanguageMismatch, quteLanguageId, onExtensionLoad, QuteSettings.QUTE_OVERRIDE_LANGUAGE_ID, QuteSettings.QUTE_TEMPLATES_LANGUAGE_MISMATCH);
260+
break;
261+
}
262+
}
263+
}
264+
}
265+
217266
interface IConfiguration {
218267
workspaceConfiguration: WorkspaceConfiguration;
219268
target: ConfigurationTarget;

src/qute/languageServer/settings.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ConfigurationTarget, workspace } from 'vscode';
2+
import { LanguageMismatch } from '../../utils/languageMismatch';
13
/**
24
* VScode Qute settings.
35
*/
@@ -17,4 +19,20 @@ export namespace QuteSettings {
1719
* Disable Qute validation for the given file name patterns settings.
1820
*/
1921
export const QUTE_VALIDATION_EXCLUDED = 'qute.validation.excluded';
22+
23+
/**
24+
* Action performed when detected Qute templates have an incorrect language.
25+
*/
26+
export const QUTE_TEMPLATES_LANGUAGE_MISMATCH = "qute.templates.languageMismatch";
27+
28+
/**
29+
* Flag for enabling forcing Qute language id setting
30+
*/
31+
export const QUTE_OVERRIDE_LANGUAGE_ID = 'qute.templates.override.languageId';
32+
33+
export function getQuteTemplatesLanguageMismatch(): QuteTemplateLanguageMismatch {
34+
return workspace.getConfiguration().get<QuteTemplateLanguageMismatch>(QUTE_TEMPLATES_LANGUAGE_MISMATCH);
35+
}
2036
}
37+
38+
export class QuteTemplateLanguageMismatch extends LanguageMismatch {}

0 commit comments

Comments
 (0)