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

Allow devs to override default snippets #1470

Closed
JasonWThompson opened this issue May 27, 2020 · 3 comments · Fixed by #1610
Closed

Allow devs to override default snippets #1470

JasonWThompson opened this issue May 27, 2020 · 3 comments · Fixed by #1610
Assignees

Comments

@JasonWThompson
Copy link

When I create a new java file, I'm able to immediately select if I want to create a class, interface, or enum. However, there does not seem to be a way to configure what said generated type looks like. Specifically for the team I work on, there's an expectation that a certain file header comment appears at the top and specific boiler plate javadoc appears before each type.

Because this isn't possible in VSCode, I often forget to put these in there manually and thus receive embarrassing comments on my code reviews about forgetting these things. 😅

In Eclipse, I could configure the template for types and files by going to Preferences->Java->Code Style->Code Templates and control what is generated. This is what I want. (This is different from Preferences->Java->Editor->Templates which is closer to VSCode snippets).

Environment
  • Operating System: Windows 10
  • JDK version: JDK 11
  • Visual Studio Code version: 1.45.1
  • Java extension version: 0.62.0
@wangchengxin
Copy link

I also want to make this suggestion, it is particularly inconvenient to use other "header" plugins, I hope developers add this feature

@fbricon
Copy link
Collaborator

fbricon commented Sep 15, 2020

  • java.templates.fileHeader: Specifies the file header comment for new Java file. Supports configuring multi-line comments with an array of strings, and using ${variable} to reference the predefined variables.

  • java.templates.typeComment: Specifies the type comment for new Java type. Supports configuring multi-line comments with an array of strings, and using ${variable} to reference the predefined variables.

Predefined variables:

  • ${file_name} - name of the current Java file
  • ${package_name} - name of the enclosing package
  • ${type_name} - name of the current type
  • ${user} - current user system login name
  • ${date} - current system date
  • ${time} - current system time
  • ${year} - current year
  • ${month} - current month
  • ${day} - current day of the month
  • ${hour} - current hour
  • ${minute} - current minute

@fbricon
Copy link
Collaborator

fbricon commented Sep 15, 2020

For instance, you can define a copyright header like:

"java.templates.fileHeader": [
  "/**",
  "*  Copyright (c) ${year} Red Hat, Inc.",
  "*  All rights reserved. This program and the accompanying materials",
  "*  are made available under the terms of the Eclipse Public License v2.0",
  "*  which accompanies this distribution, and is available at",
  "*  http://www.eclipse.org/legal/epl-v20.html",
  "*",
  "* SPDX-License-Identifier: EPL-2.0",
  "*/"
],

and a new type Javadoc like:

"java.templates.typeComment": [
  "/**",
  "* ${type_name}",
  "* @author: ${user}",
  "*/"
]

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

Successfully merging a pull request may close this issue.

4 participants