Skip to content

Commit

Permalink
Enhanced Template Java class name substitution (#522)
Browse files Browse the repository at this point in the history
* Enhanced Template Java class name substitution

* Es linting fixes

* Update user guide and package.json for default language template enhancements

* Add Java template image to user guide for better clarity
  • Loading branch information
KshKnsl authored Jan 21, 2025
1 parent 0f8b0ab commit 5bfc4eb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
Binary file added docs/img/javaTemplate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/templateSettings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ Several options are available to customize the extension. Open VS Code settings
down during codeforces submission.
- [Python] Command used to run python files. For eg. py, python3, pypy3, etc.

## Default Language Templates
- The path of the template that will be loaded when a new file of the default language is created by Competitive Companion
- For Java Users, the template shall be in the format where class name is `CLASS_NAME` as the file name so that CLASS_NAME in the code gets auto replaced.
\
![Templates](img/templateSettings.png)
![Templates](img/javaTemplate.png)


## Getting help

If you have trouble using the extension, find any bugs, or want to request a new
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@
"cph.general.defaultLanguageTemplateFileLocation": {
"type": "string",
"default": "",
"description": "The path of the template that will be loaded when a new file of the default language is created by Competitive Companion"
"description": "The path to the template file that will be used when creating a new file for the default language via Competitive Companion. For Java templates, use 'CLASS_NAME' as a placeholder for the class name like 'class CLASS_NAME{...}'"
},
"cph.general.autoShowJudge": {
"type": "boolean",
Expand Down
10 changes: 9 additions & 1 deletion src/companion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,16 @@ const handleNewProblem = async (problem: Problem) => {
`Template file does not exist: ${templateLocation}`,
);
} else {
const templateContents =
let templateContents =
readFileSync(templateLocation).toString();

if (extn == 'java') {
const className = path.basename(problemFileName, '.java');
templateContents = templateContents.replace(
'CLASS_NAME',
className,
);
}
writeFileSync(srcPath, templateContents);
}
}
Expand Down

0 comments on commit 5bfc4eb

Please sign in to comment.