-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Senior-Capstone-2024/topic-findCodeDupes
added java convention for duplicate code
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
export function findDuplicateCode() { | ||
const editor = vscode.window.activeTextEditor; | ||
if (editor) { | ||
const document = editor.document; | ||
const text = document.getText(); | ||
const lines = text.split('\n'); | ||
const duplicates: Map<string, number[]> = new Map(); | ||
|
||
// Iterate through each line of the document | ||
lines.forEach((line, index) => { | ||
// Match method signatures | ||
const methodNameMatch = line.match(/\b(public|private|protected)?\s+(\w+)\s+(\w+)\s*\(/); | ||
if (methodNameMatch && methodNameMatch[3]) { | ||
const methodNameLower = methodNameMatch[3].toLowerCase(); | ||
// Store line number in the map of duplicates | ||
if (duplicates.has(methodNameLower)) { | ||
duplicates.get(methodNameLower)!.push(index + 1); | ||
} else { | ||
duplicates.set(methodNameLower, [index + 1]); | ||
} | ||
} | ||
|
||
// Match class/interface declarations | ||
const classInterfaceNameMatch = line.match(/\b(class|interface)\s+(\w+)/); | ||
if (classInterfaceNameMatch && classInterfaceNameMatch[2]) { | ||
const classNameLower = classInterfaceNameMatch[2].toLowerCase(); | ||
// Store line number in the map of duplicates | ||
if (duplicates.has(classNameLower)) { | ||
duplicates.get(classNameLower)!.push(index + 1); | ||
} else { | ||
duplicates.set(classNameLower, [index + 1]); | ||
} | ||
} | ||
}); | ||
|
||
// Show information message with duplicate lines | ||
if (duplicates.size > 0) { | ||
let message = 'Duplicate code found on lines: '; | ||
// Construct message with duplicate lines | ||
duplicates.forEach((lineNumbers, name) => { | ||
message += `${name}: `; | ||
message += lineNumbers.map(num => num.toString()).join(', '); | ||
message += '; '; | ||
}); | ||
message = message.slice(0, -2); // Remove the trailing space and semicolon | ||
vscode.window.showInformationMessage(message); | ||
// Highlight duplicate lines in the editor | ||
highlightLines(editor, duplicates); | ||
} else { | ||
vscode.window.showInformationMessage('No duplicate code found.'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Highlights duplicate lines in the editor. | ||
* @param editor The active text editor. | ||
* @param duplicates A map containing duplicate lines. | ||
*/ | ||
function highlightLines(editor: vscode.TextEditor, duplicates: Map<string, number[]>) { | ||
const document = editor.document; | ||
const decorations: vscode.DecorationOptions[] = []; | ||
|
||
// Iterate through each entry in the map of duplicates | ||
duplicates.forEach(lineNumbers => { | ||
lineNumbers.forEach(lineNumber => { | ||
// Get the line and range corresponding to each duplicate line number | ||
const line = document.lineAt(lineNumber - 1); | ||
const range = line.range; | ||
// Create decoration options for each duplicate line | ||
decorations.push({ | ||
range: range, | ||
renderOptions: { | ||
// No decoration options needed | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
// Set decorations in the editor to highlight the duplicate lines | ||
editor.setDecorations(vscode.window.createTextEditorDecorationType({ | ||
backgroundColor: 'rgba(255, 128, 0, 0.3)' | ||
}), decorations); | ||
} |