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

Activate extension when Java files are found in src #3940

Open
jrstanley opened this issue Feb 3, 2025 · 1 comment
Open

Activate extension when Java files are found in src #3940

jrstanley opened this issue Feb 3, 2025 · 1 comment

Comments

@jrstanley
Copy link

Problem

I am using this extension as part of the recommended by VSCode Extension Pack for Java extension.

When using the above extension pack to create a new Java project with no build tools, a project is initialised with a default src/App.java entry point.

If I create a new .java file with this vscode-java extension enabled, I helpfully get an outline/snippet inserted into the document for creating a new class (or interface etc.) based on the filename.

However, this is reliant on the vscode-java extension being activated. In the case of a project with no build tools, and therefore not meeting any of the existing activationEvents, then the extension only activates when an existing file using the Java language has been opened.

In the case that the first action is creating a new .java file, the file is created and the extension launches but without inserting the outline/snipped for the class (or interface etc.).

This behaviour can appear to end-users that VSCode sometimes inserts the outline for a Java class and sometimes does not.

Possible solutions

I can think of two possible solutions.

  1. Expand activationEvents to activate the extension when .java file exists somewhere in the project. I am proposing a pattern of src/**/*.java to match src/App.java or src/module/App.java. With this change the extension will activate when the workspace opens, rather than being reliant on a .java file being opened first.

  2. When the extension is activated, check to see if there is an open (or there are existing) .java file(s) which are empty and apply the outline/snipped based on the same conditions as already exist.

Proposed solution

I am proposing a solution based on (1) above, which will launch the extension where a .java file exists within a src directory. I have chosen this because it matches the default project structure created when creating a new Java project with no build tools using the extension pack above.

A limitation of this solution is that it will not work for all project structures. I would be open to modifying this so that the extension is enabled if .java files exist anywhere in the workspace, however I am not clear if there are drawbacks to this approach.

@rgrunber
Copy link
Member

rgrunber commented Feb 5, 2025

You're referring to this right ?

server-snippets-not-available.mp4

I guess the workaround is the class snippet once it finishes loading. We put quite a bit of work into making the snippets much better. The real problem, as you've discovered is that if the the activation event for the extension is the file creation, we're already too late to register our client-side logic 😆

if (workspace.onDidCreateFiles) {// Theia doesn't support workspace.onDidCreateFiles yet
context.subscriptions.push(workspace.onDidCreateFiles(handleNewJavaFiles));
}
.

Only activating when build files are detected or a Java source file is opened has been the expected behaviour for a while. I suspect because as an installed extension, we'd like to activate only when we're absolutely sure we're needed. When a project contains known build files (pom.xml, build.gradle, .classpath), we know we should activate, and do so purely through detecting those files. When there's only Java source files, it's more difficult. Are they sample files, or maybe the user doesn't want Java support right now. The safer way is to activate when a Java source file is actually opened, as that makes it more clear. The downside is the delayed activation means that certain features may not be immediately available, or we won't register handlers in time.

Update: I think I like (2) a little more, as it avoids having us activate more often.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants