Skip to content

Commit 58da61f

Browse files
Merge pull request #375 from N1ebieski/Fix-False-positive-Translation-not-found-with-partial-string-in-Laravel-JSON-translation-#38
Fix False positive "Translation not found" with partial string in Laravel JSON translation
2 parents 50cdb75 + 2878325 commit 58da61f

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/features/translation.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { notFound } from "@src/diagnostic";
22
import AutocompleteResult from "@src/parser/AutocompleteResult";
33
import {
4+
getTranslationItemByName,
45
getTranslations,
56
TranslationItem,
67
} from "@src/repositories/translations";
@@ -121,8 +122,7 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
121122
return null;
122123
}
123124

124-
const translation =
125-
getTranslations().items.translations[param.value];
125+
const translation = getTranslationItemByName(param.value);
126126

127127
if (!translation) {
128128
return null;
@@ -148,7 +148,7 @@ export const hoverProvider: HoverProvider = (
148148
pos: vscode.Position,
149149
): vscode.ProviderResult<vscode.Hover> => {
150150
return findHoverMatchesInDoc(doc, pos, toFind, getTranslations, (match) => {
151-
const item = getTranslations().items.translations[match];
151+
const item = getTranslationItemByName(match);
152152

153153
if (!item) {
154154
return null;
@@ -184,7 +184,7 @@ export const diagnosticProvider = (
184184
return null;
185185
}
186186

187-
const item = getTranslations().items.translations[param.value];
187+
const item = getTranslationItemByName(param.value);
188188

189189
if (item) {
190190
return null;
@@ -252,6 +252,15 @@ export const completionProvider = {
252252
getTranslations().items.translations,
253253
).length;
254254

255+
const precedingCharacter = document.getText(
256+
new vscode.Range(
257+
position.line,
258+
position.character - 1,
259+
position.line,
260+
position.character,
261+
),
262+
);
263+
255264
return Object.entries(getTranslations().items.translations).map(
256265
([key, translations]) => {
257266
let completionItem = new vscode.CompletionItem(
@@ -264,6 +273,12 @@ export const completionProvider = {
264273
wordMatchRegex,
265274
);
266275

276+
if (precedingCharacter === "'") {
277+
completionItem.insertText = key.replaceAll("'", "\\'");
278+
} else if (precedingCharacter === '"') {
279+
completionItem.insertText = key.replaceAll('"', '\\"');
280+
}
281+
267282
if (totalTranslationItems < 200) {
268283
// This will bomb if we have too many translations,
269284
// 200 is an arbitrary but probably safe number

src/repositories/translations.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ const load = () => {
7070
});
7171
};
7272

73+
export const getTranslationItemByName = (match: string): TranslationItem | undefined => {
74+
return getTranslations().items.translations[match.replaceAll('\\', '')];
75+
};
76+
7377
export const getTranslations = repository<TranslationGroupResult>({
7478
load,
7579
pattern: () =>

src/support/doc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export const findHoverMatchesInDoc = (
6969
): ProviderResult<Hover> => {
7070
const linkRange = doc.getWordRangeAtPosition(
7171
pos,
72-
new RegExp(/(['"])(.*?)\1/),
72+
new RegExp(/(?<!\\)(['"])(.*?)(?<!\\)\1/),
7373
);
7474

7575
if (!linkRange) {

0 commit comments

Comments
 (0)