Skip to content

Commit c82de98

Browse files
committed
exact matching of keywords with regexp
1 parent af5bade commit c82de98

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

extension.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ var indentString = require('indent-string');
44

55
import * as parser from './argParser';
66

7+
// escape all regular expression tags from text to be matched
8+
const escapeRegExpMatch = function(txt: string) {
9+
return txt.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
10+
};
11+
12+
// check if a keyword is in a string exactly or not
13+
const isExactMatch = (txt: string, keyword: string) => {
14+
return new RegExp(`\\b${escapeRegExpMatch(keyword)}\\b`).test(txt)
15+
}
16+
717
// given a line number in the code with a function or modifier keyword
818
// this function expands the text until an opening '{' or ending ';'
919
export function expandToOpeningBracket(cNo: number): string {
@@ -73,12 +83,15 @@ export function activate(ctx:vscode.ExtensionContext) {
7383
var lineAt = vscode.window.activeTextEditor.document.lineAt(currentLineNo);
7484
var textAtCurrentLine = lineAt.text.trim();
7585

76-
if(textAtCurrentLine.includes('contract') || textAtCurrentLine.includes('interface') || textAtCurrentLine.includes('library')) {
86+
if (isExactMatch(textAtCurrentLine, 'contract') ||
87+
isExactMatch(textAtCurrentLine, 'interface') ||
88+
isExactMatch(textAtCurrentLine, 'library')
89+
) {
7790
insertText("/**\n * @author .\n * @title .\n * @dev .\n * @notice .\n */\n");
7891
return;
7992
}
8093

81-
if(textAtCurrentLine.includes('function') || textAtCurrentLine.includes('modifier')) {
94+
if(isExactMatch(textAtCurrentLine, 'function') || isExactMatch(textAtCurrentLine, 'modifier')) {
8295
selectedText = textAtCurrentLine;
8396
if (!selectedText.includes('{') && !selectedText.includes(';')) {
8497
selectedText = expandToOpeningBracket(currentLineNo)

0 commit comments

Comments
 (0)