Skip to content

Commit ce7a2c1

Browse files
committed
refactor: enhance citation key pattern matching and validation
1 parent b2302be commit ce7a2c1

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

jabls/src/main/java/org/jabref/languageserver/util/definition/DefinitionProvider.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
public abstract class DefinitionProvider {
2121

22-
private static final Pattern CITATION_KEY_PATTERN = Pattern.compile("@[a-z0-9_\\-.+:]+", Pattern.CASE_INSENSITIVE);
22+
private static final Pattern CITATION_KEY_PATTERN = Pattern.compile("@(?<citationkey>[a-z0-9_.+:-]+)", Pattern.CASE_INSENSITIVE);
23+
private static final Pattern VALID_BEFORE_AT = Pattern.compile("[\\s\\[({;,:\\-—–«“\"'’]?");
24+
private static final Pattern CITATION_KEY_CHAR_PATTERN = Pattern.compile("[a-z0-9_\\-:.+]", Pattern.CASE_INSENSITIVE);
2325

2426
protected final LspParserHandler parserHandler;
2527

@@ -44,7 +46,7 @@ public List<DocumentLink> provideDocumentLinks(String content) {
4446
Matcher matcher = CITATION_KEY_PATTERN.matcher(content);
4547
return matcher.results()
4648
.map(matchResult -> {
47-
String citationKey = matchResult.group().substring(1); // remove leading '@'
49+
String citationKey = matchResult.group("citationkey");
4850
Range range = LspRangeUtil.convertToLspRange(content, matchResult.start(), matchResult.end());
4951
DocumentLink documentLink = new DocumentLink();
5052
documentLink.setRange(range);
@@ -117,26 +119,14 @@ private int clamp(int i, int min, int max) {
117119
}
118120

119121
private boolean isCitationKeyCharacter(char c) {
120-
return (c >= 'A' && c <= 'Z')
121-
|| (c >= 'a' && c <= 'z')
122-
|| (c >= '0' && c <= '9')
123-
|| c == '_' || c == '-' || c == ':' || c == '.' || c == '+';
122+
return CITATION_KEY_CHAR_PATTERN.matcher(String.valueOf(c)).matches();
124123
}
125124

126125
private boolean isValidCitationKeyCharBefore(String content, int idBeforeAt) {
127126
if (idBeforeAt < 0) {
128127
return true;
129128
}
130-
char p = content.charAt(idBeforeAt);
131-
132-
if (Character.isLetterOrDigit(p) || p == '_' || p == '.') {
133-
return false;
134-
}
135-
136-
return Character.isWhitespace(p)
137-
|| p == '[' || p == '(' || p == '{'
138-
|| p == ';' || p == ',' || p == ':'
139-
|| p == '-' || p == '—' || p == '–' || p == '«' || p == '“' || p == '"'
140-
|| p == '\'';
129+
String before = content.substring(idBeforeAt, idBeforeAt + 1);
130+
return VALID_BEFORE_AT.matcher(before).matches();
141131
}
142132
}

0 commit comments

Comments
 (0)