From dc6b74677a352156cfda3d6023a6156038631b9e Mon Sep 17 00:00:00 2001 From: willeand Date: Thu, 2 May 2024 00:20:43 -0700 Subject: [PATCH] Highlights constants with improper naming convention --- package.json | 6 +++++- src/extension.ts | 2 ++ src/findConstantCap.ts | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/findConstantCap.ts diff --git a/package.json b/package.json index db0c240..bfa83c4 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,11 @@ { "command": "extension.findAllErrors", "title": "findAllErrors" - } + }, + { + "command": "extension.findConstantCap", + "title": "findConstantCap" + } ], "menus": { "commandPalette": [ diff --git a/src/extension.ts b/src/extension.ts index b2b7a88..85e9923 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -7,6 +7,7 @@ import { findLowercaseClassOrInterface } from './findLowercaseClassOrInterface'; import { findCapitalizedMethodName } from './findCapitalizedMethodName'; import { singleStatementPerLineChecker } from './singleStatementPerLineChecker'; import { findAllErrors } from './findAllErrors'; +import { findConstantCap} from './findConstantCap'; export function activate(context: vscode.ExtensionContext) { console.log('Congratulations, your extension "Java convention commands" is now active!'); @@ -16,6 +17,7 @@ export function activate(context: vscode.ExtensionContext) { const disposable2 = vscode.commands.registerCommand('extension.findCapitalizedMethodName', findCapitalizedMethodName); const disposable3 = vscode.commands.registerCommand('extension.singleStatementPerLineChecker', singleStatementPerLineChecker); const disposable0 = vscode.commands.registerCommand('extension.findAllErrors', findAllErrors); + const disposable4 = vscode.commands.registerCommand('extension.findConstantCap', findConstantCap); const commentController = vscode.comments.createCommentController('comment-sample', 'Comment API Sample'); diff --git a/src/findConstantCap.ts b/src/findConstantCap.ts new file mode 100644 index 0000000..5ba7cbe --- /dev/null +++ b/src/findConstantCap.ts @@ -0,0 +1,37 @@ +import * as vscode from 'vscode'; + +export function findConstantCap() { + // activeTextEditor allows access to text inside the opened document. + const editor = vscode.window.activeTextEditor; + if (editor) { + const document = editor.document; + const text = document.getText(); + const constantRegex = /(? 0) { + editor.setDecorations(improperConstantNameDecorationType, improperConstants); + } else { + vscode.window.showInformationMessage('All constants follow proper naming conventions!'); + } + } +} + +const improperConstantNameDecorationType = vscode.window.createTextEditorDecorationType({ + backgroundColor: 'rgba(255, 0, 0, 0.3)' +}); + +function isValidConstantName(constantName: string): boolean { + const pattern: RegExp = /^[A-Z][A-Z0-9_]*$/; + return pattern.test(constantName); +}