Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize custom file extensions #3731

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"],
rgrunber marked this conversation as resolved.
Show resolved Hide resolved
},
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") {
rgrunber marked this conversation as resolved.
Show resolved Hide resolved
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