Skip to content

Activate extension when Java files are found in src #3940

Open
@jrstanley

Description

@jrstanley

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions