Skip to content

Commit

Permalink
Merge pull request #12 from Senior-Capstone-2024/findConstantCap
Browse files Browse the repository at this point in the history
Highlights constants with improper naming convention
  • Loading branch information
aravindrsripada authored May 2, 2024
2 parents f015be9 + dc6b746 commit d248806
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@
{
"command": "extension.findAllErrors",
"title": "findAllErrors"
}
},
{
"command": "extension.findConstantCap",
"title": "findConstantCap"
}
],
"menus": {
"commandPalette": [
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand All @@ -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');
Expand Down
37 changes: 37 additions & 0 deletions src/findConstantCap.ts
Original file line number Diff line number Diff line change
@@ -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 = /(?<!\w)final\s+\w+\s+(\w+)\s*=/g;
let match;
const improperConstants = [];
while ((match = constantRegex.exec(text))) {
const constantName = match[1];
if (!isValidConstantName(constantName)) {
const constantStartPosition = document.positionAt(match.index);
const constantEndPosition = document.positionAt(match.index + constantName.length);
const range = new vscode.Range(constantStartPosition, constantEndPosition);
improperConstants.push(range);
vscode.window.showInformationMessage('Improper constant naming convention! Highlighted in RED');
}
}
if (improperConstants.length > 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);
}

0 comments on commit d248806

Please sign in to comment.