Skip to content

Commit 33877a6

Browse files
committed
2 parents c7117aa + 58da61f commit 33877a6

File tree

8 files changed

+41
-14
lines changed

8 files changed

+41
-14
lines changed

php-templates/configs.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ function vsCodeGetConfigValue($value, $key, $configPaths) {
117117
}
118118
}
119119

120+
if (is_object($value)) {
121+
$value = get_class($value);
122+
}
123+
120124
return [
121125
"name" => $key,
122126
"value" => $value,

php-templates/translations.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ protected function linesFromJsonFile($file)
129129

130130
$lines = explode(PHP_EOL, $contents);
131131
$encoded = array_map(
132-
fn($k) => [json_encode($k), $k],
132+
fn($k) => [json_encode($k, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), $k],
133133
array_keys($json),
134134
);
135135
$result = [];
136-
$searchRange = 2;
136+
$searchRange = 5;
137137

138138
foreach ($encoded as $index => $keys) {
139139
// Pretty likely to be on the line that is the index, go happy path first
@@ -142,7 +142,7 @@ protected function linesFromJsonFile($file)
142142
continue;
143143
}
144144

145-
// Search around the index, like to be within 2 lines
145+
// Search around the index, likely to be within $searchRange lines
146146
$start = max(0, $index - $searchRange);
147147
$end = min($index + $searchRange, count($lines) - 1);
148148
$current = $start;
@@ -364,4 +364,4 @@ protected function fromPhpFile($file, $path, $namespace)
364364
'values' => array_keys($translator->values),
365365
'params' => array_map(fn($p) => json_decode($p, true), array_keys($translator->paramResults)),
366366
'to_watch' => $translator->directoriesToWatch,
367-
]);
367+
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

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) {

src/support/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export const detect = async (
147147

148148
const promise = runCommand(`detect "${cleanArg(code)}"`)
149149
.then((result: string) => {
150-
return JSON.parse(result);
150+
return result.length > 0 ? JSON.parse(result) : result;
151151
})
152152
.catch((err) => {
153153
showErrorPopup(err);

src/templates/configs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ function vsCodeGetConfigValue($value, $key, $configPaths) {
117117
}
118118
}
119119
120+
if (is_object($value)) {
121+
$value = get_class($value);
122+
}
123+
120124
return [
121125
"name" => $key,
122126
"value" => $value,

src/templates/translations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ $translator = new class
129129
130130
$lines = explode(PHP_EOL, $contents);
131131
$encoded = array_map(
132-
fn($k) => [json_encode($k), $k],
132+
fn($k) => [json_encode($k, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), $k],
133133
array_keys($json),
134134
);
135135
$result = [];
136-
$searchRange = 2;
136+
$searchRange = 5;
137137
138138
foreach ($encoded as $index => $keys) {
139139
// Pretty likely to be on the line that is the index, go happy path first
@@ -142,7 +142,7 @@ $translator = new class
142142
continue;
143143
}
144144
145-
// Search around the index, like to be within 2 lines
145+
// Search around the index, likely to be within $searchRange lines
146146
$start = max(0, $index - $searchRange);
147147
$end = min($index + $searchRange, count($lines) - 1);
148148
$current = $start;
@@ -364,5 +364,5 @@ echo json_encode([
364364
'values' => array_keys($translator->values),
365365
'params' => array_map(fn($p) => json_decode($p, true), array_keys($translator->paramResults)),
366366
'to_watch' => $translator->directoriesToWatch,
367-
]);
367+
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
368368
`;

0 commit comments

Comments
 (0)