Skip to content

Commit

Permalink
Merge pull request #23 from Senior-Capstone-2024/longMethod
Browse files Browse the repository at this point in the history
Code Smell Long-Method Completed version 1.0
  • Loading branch information
willeand authored Jun 4, 2024
2 parents 63140a4 + 833a75d commit 5790ecc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion output/data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"3":["Improper Capitalized Primitive Type"],"4":["Improper Capitalized Primitive Type"],"7":["Lowercase ENUM found!"],"18":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"19":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"20":["Improper Capitalized Primitive Type"],"35":["Improper Capitalized Primitive Type"],"179":["enum fruit"]}
{"3":["Improper Capitalized Primitive Type"],"4":["Improper Capitalized Primitive Type"],"12":["Code Smell: Consider refactoring to reduce method length"],"15":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"16":["Improper Capitalized Primitive Type","Improper Capitalized Primitive Type"],"17":["Improper Capitalized Primitive Type"],"31":["Code Smell: Consider refactoring to reduce method length"],"50":["Improper Capitalized Primitive Type"]}
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
{
"command": "extension.findDuplicateCode",
"title": "findDuplicateCode"
},
{
"command": "extension.longMethod",
"title": "findLongMethod"
}
],
"menus": {
Expand Down
7 changes: 6 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { convertMapToJson } from './convertMapToJson';
import { findDuplicateCode } from './findDuplicateCode';
import { findLowercaseEnums } from './findLowercaseEnums';
import { findConstantCap} from './findConstantCap';
import { longMethod } from './longMethod';

const myMap: Map<number, string[]> = new Map();
const filePath = 'C:\\Users\\Will\\Desktop\\cww\\gradeFast-1.0\\output\\data.json';
const filePath = 'C:\\Users\\Will\\Desktop\\cw\\gradeFast-1.0\\output\\data.json';


export function activate(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -54,5 +55,9 @@ export function activate(context: vscode.ExtensionContext) {
}
};

const disposable7 = vscode.commands.registerCommand('extension.longMethod', () => {
longMethod(myMap);
} );

NoteCommentController.registerCommands(context);
}
60 changes: 60 additions & 0 deletions src/longMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as vscode from 'vscode';

export function longMethod(myMap: Map<number, string[]>): Map<number, string[]> {
// Check for active text editor
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No active text editor.');
return myMap; // Early return if no editor
}

// Get the document text
const document = editor.document;
const text = document.getText();

// Pattern to identify method definitions
const methodPattern = /(\b(?:public|protected|private|static|\s)+[^\s]+\s+[^\s]+\s*\([^)]*\)\s*{)/g;

let match;
while ((match = methodPattern.exec(text)) !== null) {
const startPos = document.positionAt(match.index);
const startLine = startPos.line;

// Count the number of lines in the method
const methodLines = countMethodLines(document, startLine);

if (methodLines > 10) {
const errorMessage = "Code Smell: Consider refactoring to reduce method length";
if (!myMap.has(startLine)) {
myMap.set(startLine, []);
}
const currentErrors = myMap.get(startLine) || [];
currentErrors.push(errorMessage);
myMap.set(startLine, currentErrors);
}
}

return myMap;
}

// Helper function to count method lines
function countMethodLines(document: vscode.TextDocument, startLine: number): number {
let lineNumber = startLine;
let methodOpen = true;
let openBraces = 0;
let closeBraces = 0;

while (lineNumber < document.lineCount && methodOpen) {
const lineText = document.lineAt(lineNumber).text.trim();
openBraces += (lineText.match(/{/g) || []).length;
closeBraces += (lineText.match(/}/g) || []).length;

if (closeBraces > 0 && openBraces === closeBraces) {
methodOpen = false;
}

lineNumber++;
}

return lineNumber - startLine;
}

0 comments on commit 5790ecc

Please sign in to comment.