Skip to content

Commit 90d1622

Browse files
committed
Other small fixes
1 parent cacd6f2 commit 90d1622

1 file changed

Lines changed: 10 additions & 22 deletions

File tree

content.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,46 +75,34 @@ chrome.runtime.onMessage.addListener(function (msg) {
7575
}
7676
}
7777
} else if (msg.ext === "kt") {
78-
var code = $(el[j]).html();
79-
80-
// The workaround for Kotlin highlights imports too. Skip them, both Kotlin and Java (faster loop)
81-
if (code.startsWith("import ")) continue;
78+
var codeHtml = $(el[j]).html();
79+
var codeText = $(el[j]).text();
8280

8381
/*
8482
Workaround for Kotlin. It gets the first char of a class (first index); it checks if before the class name there's another char, if true skip this line;
8583
it calculates the used length, adding to the first index, the length of class name, plus a special char (like . ( < >).
8684
At the end it checks if the substring matches with a valid usage of class name, so replace it with the URL
8785
*/
88-
var index = code.indexOf(clazz.className);
86+
var indexHtml = codeHtml.indexOf(clazz.className);
87+
var indexText = codeText.indexOf(clazz.className);
8988

90-
// TODO: this check in regex?
91-
switch (code.charAt(index - 1)) {
92-
case "<":
93-
case " ":
94-
case "(":
95-
break;
96-
default:
97-
continue;
98-
}
99-
100-
var classUsage = index + clazz.className.length + 1;
101-
// TODO: this check in regex?
102-
switch (code.charAt(classUsage - 1)) {
89+
switch (codeText.charAt(indexText - 1)) {
10390
case "<":
10491
case ">":
10592
case " ":
10693
case "(":
107-
case ")":
108-
case ".":
10994
break;
11095
default:
11196
continue;
11297
}
11398

114-
var result = code.substr(index, classUsage).match(REGEX_VALID_USAGE_OF_CLASS_NAME);
99+
var classUsage = indexHtml + clazz.className.length + 1;
100+
if (codeHtml.charAt(classUsage - 1) >= 'A' || 'Z' <= codeHtml.charAt(classUsage - 1)) continue;
101+
102+
var result = codeHtml.substr(indexHtml, classUsage).match(REGEX_VALID_USAGE_OF_CLASS_NAME);
115103

116104
if (result) {
117-
$(el[j]).html(code.replaceBetween(index, classUsage - 1, '<a href=\"' + clazz.getLinkedUrl() + '\" target=\"_blank\">' + clazz.className + '</a>'));
105+
$(el[j]).html(codeHtml.replaceBetween(indexHtml, classUsage - 1, '<a href=\"' + clazz.getLinkedUrl() + '\" target=\"_blank\">' + clazz.className + '</a>'));
118106
}
119107
}
120108
}

0 commit comments

Comments
 (0)