Skip to content

Commit

Permalink
Recognize custom file extensions (#3731)
Browse files Browse the repository at this point in the history
  • Loading branch information
snjeza committed Aug 9, 2024
1 parent e638d27 commit e12955b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
{ scheme: 'untitled', language: 'java' }
],
synchronize: {
configurationSection: ['java', 'editor.insertSpaces', 'editor.tabSize'],
configurationSection: ['java', 'editor.insertSpaces', 'editor.tabSize', "files.associations"],
},
initializationOptions: {
bundles: collectJavaExtensions(extensions.all),
Expand Down Expand Up @@ -1061,7 +1061,7 @@ async function getTriggerFiles(): Promise<string[]> {
function getJavaFilePathOfTextDocument(document: TextDocument): string | undefined {
if (document) {
const resource = document.uri;
if (resource.scheme === 'file' && resource.fsPath.endsWith('.java')) {
if (resource.scheme === 'file' && document.languageId === "java") {
return path.normalize(resource.fsPath);
}
}
Expand Down
20 changes: 19 additions & 1 deletion src/fileEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,25 @@ function getWillRenameHandler(client: LanguageClient) {
}

function isJavaFile(uri: Uri): boolean {
return uri.fsPath && uri.fsPath.endsWith(".java");
if (uri.fsPath && uri.fsPath.endsWith(".java")) {
return true;
}
let result = false;
const associations = workspace.getConfiguration().get("files.associations");
if (associations !== null) {
Object.keys(associations).forEach(pattern => {
const langId = associations[pattern];
if (langId === 'java' && pattern.startsWith('*.') && pattern.length > 2) {
const ext = pattern.substring(2);
if (!ext.includes('?') && !ext.includes('*')) {
if (uri.fsPath && uri.fsPath.endsWith(`.${ext}`)) {
result = true;
}
}
}
});
}
return result;
}

async function isFile(uri: Uri): Promise<boolean> {
Expand Down
4 changes: 2 additions & 2 deletions src/pasteAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export async function registerOrganizeImportsOnPasteCommand(): Promise<void> {
});

action.then((wasApplied) => {
const fileURI = editor.document.uri.toString();
if (wasApplied && fileURI.endsWith(".java")) {
if (wasApplied && editor.document.languageId === "java") {
const fileURI = editor.document.uri.toString();
const hasText: boolean = documentText !== null && /\S/.test(documentText);
if (hasText) {
// Organize imports silently to avoid surprising the user
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ export async function getJavaConfig(javaHome: string) {
const editorConfig = workspace.getConfiguration('editor');
javaConfig.format.insertSpaces = editorConfig.get('insertSpaces');
javaConfig.format.tabSize = editorConfig.get('tabSize');
const filesConfig = workspace.getConfiguration('files');
javaConfig.associations = filesConfig.get('associations');
const isInsider: boolean = version.includes("insider");
const androidSupport = javaConfig.jdt.ls.androidSupport.enabled;
switch (androidSupport) {
Expand Down

0 comments on commit e12955b

Please sign in to comment.