@@ -17,11 +17,12 @@ import * as path from 'path';
1717import { commands , ConfigurationChangeEvent , Disposable , ExtensionContext , extensions , languages , Terminal , TextDocument , window , workspace } from 'vscode' ;
1818import { registerVSCodeCommands } from './commands/registerCommands' ;
1919import { ProjectLabelInfo } from './definitions/ProjectLabelInfo' ;
20- import { PropertiesLanguageMismatch , QuarkusConfig } from './QuarkusConfig' ;
20+ import { QuarkusPropertiesLanguageMismatch , QuarkusConfig } from './QuarkusConfig' ;
2121import { QuarkusContext } from './QuarkusContext' ;
2222import quarkusProjectListener from './QuarkusProjectListener' ;
2323import { connectToQuteLS } from './qute/languageServer/client' ;
2424import { terminalCommandRunner } from './terminal/terminalCommandRunner' ;
25+ import { tryToForceLanguageId } from './utils/languageMismatch' ;
2526import { JAVA_EXTENSION_ID } from './utils/requestStandardMode' ;
2627import { initTelemetryService } from './utils/telemetryUtils' ;
2728import { 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-
14388const APP_PROPERTIES_PATTERN = / ^ a p p l i c a t i o n (?: - [ A - Z a - z ] + ) \. p r o p e r t i e s $ / ;
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
0 commit comments