From 48d71767a0e68d6b4e171d7e5af0bd398c479d57 Mon Sep 17 00:00:00 2001 From: willeand Date: Thu, 11 Apr 2024 11:03:19 -0700 Subject: [PATCH] Creates a new java convention for finding multiple statements per line --- package.json | 4 ++++ src/extension.ts | 2 ++ src/singleStatementPerLineChecker.ts | 32 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/singleStatementPerLineChecker.ts diff --git a/package.json b/package.json index fba97de..a3a9ca4 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,10 @@ { "command": "extension.findCapitalizedMethodName", "title": "findCapitalizedMethodName" + }, + { + "command": "extension.singleStatementPerLineChecker", + "title": "singleStatementPerLineChecker" } ], "menus": { diff --git a/src/extension.ts b/src/extension.ts index 23adbc1..2512d9c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,6 +5,7 @@ import { NoteComment, NoteCommentController } from './noteComment'; import { findCapitalizedPrimitiveTypes } from './findCapitalizedPrimitiveTypes'; import { findLowercaseClassOrInterface } from './findLowercaseClassOrInterface'; import { findCapitalizedMethodName } from './findCapitalizedMethodName'; +import { singleStatementPerLineChecker } from './singleStatementPerLineChecker'; export function activate(context: vscode.ExtensionContext) { console.log('Congratulations, your extension "Java convention commands" is now active!'); @@ -12,6 +13,7 @@ export function activate(context: vscode.ExtensionContext) { const disposable = vscode.commands.registerCommand('extension.findCapitalizedPrimitiveTypes', findCapitalizedPrimitiveTypes); const disposable1 = vscode.commands.registerCommand('extension.findLowercaseClassOrInterface', findLowercaseClassOrInterface); const disposable2 = vscode.commands.registerCommand('extension.findCapitalizedMethodName', findCapitalizedMethodName); + const disposable3 = vscode.commands.registerCommand('extension.singleStatementPerLineChecker', singleStatementPerLineChecker); const commentController = vscode.comments.createCommentController('comment-sample', 'Comment API Sample'); context.subscriptions.push(commentController); diff --git a/src/singleStatementPerLineChecker.ts b/src/singleStatementPerLineChecker.ts new file mode 100644 index 0000000..1291bd9 --- /dev/null +++ b/src/singleStatementPerLineChecker.ts @@ -0,0 +1,32 @@ +import * as vscode from 'vscode'; + +export function singleStatementPerLineChecker(): number[] { + const problematicLines: number[] = []; // Array to store line numbers with multiple semicolons + + // Get the currently active text editor + const editor = vscode.window.activeTextEditor; + + // If there is an active text editor + if (editor) { + const document = editor.document; + const text = document.getText(); + + // Split the text into an array of lines + const lines = text.split('\n'); + + lines.forEach((lineText, lineNumber) => { + + const isForLoop = /^\s*for\s*\([^;]*;[^;]*;[^;]*\)\s*{/i.test(lineText); + + if (!isForLoop){ + const semicolonCount = (lineText.match(/;/g) || []).length; + if (semicolonCount > 1) { + // Add the line number to the array + problematicLines.push(lineNumber + 1); // Add 1 because line numbers start from 1 + } + }}); + } + vscode.window.showInformationMessage(`Problematic lines: ${problematicLines.join(', ')}`); + return problematicLines; + +}