Skip to content

Commit d03d9e0

Browse files
Merge pull request #415 from N1ebieski:Support-for-linked-parameter-vscodeDiagnosticcode-for-diagnosticProvider-#46
Support for linked parameter vscode.Diagnostic.code for diagnosticProvider
2 parents 1ded13e + 2ec6733 commit d03d9e0

File tree

6 files changed

+119
-45
lines changed

6 files changed

+119
-45
lines changed

src/diagnostic/index.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
import { Diagnostic, DiagnosticSeverity, Range } from "vscode";
1+
import { Diagnostic, DiagnosticSeverity, Range, Uri } from "vscode";
22

3-
export const notFound = (
4-
descriptor: string,
5-
match: string,
6-
range: Range,
7-
code: DiagnosticCode,
8-
): Diagnostic => ({
9-
message: `${descriptor} [${match}] not found.`,
10-
severity: DiagnosticSeverity.Warning,
11-
range,
12-
source: "Laravel Extension",
13-
code,
14-
});
3+
export type DiagnosticCodeWithTarget = {
4+
value: DiagnosticCode;
5+
target: Uri;
6+
};
157

168
export type DiagnosticCode =
179
| "appBinding"
@@ -27,3 +19,18 @@ export type DiagnosticCode =
2719
| "translation"
2820
| "view"
2921
| "storage_disk";
22+
23+
export type NotFoundCode = DiagnosticCode | DiagnosticCodeWithTarget;
24+
25+
export const notFound = (
26+
descriptor: string,
27+
match: string,
28+
range: Range,
29+
code: NotFoundCode,
30+
): Diagnostic => ({
31+
message: `${descriptor} [${match}] not found.`,
32+
severity: DiagnosticSeverity.Warning,
33+
range,
34+
source: "Laravel Extension",
35+
code,
36+
});

src/features/config.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { notFound } from "@src/diagnostic";
1+
import { notFound, NotFoundCode } from "@src/diagnostic";
22
import AutocompleteResult from "@src/parser/AutocompleteResult";
3-
import { getConfigs } from "@src/repositories/configs";
3+
import { getConfigPathByName, getConfigs } from "@src/repositories/configs";
44
import { config } from "@src/support/config";
55
import { findHoverMatchesInDoc } from "@src/support/doc";
66
import { detectedRange, detectInDoc } from "@src/support/parser";
@@ -75,7 +75,7 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
7575
return null;
7676
}
7777

78-
const configItem = getConfigs().items.find(
78+
const configItem = getConfigs().items.configs.find(
7979
(config) => config.name === param.value,
8080
);
8181

@@ -107,7 +107,7 @@ export const hoverProvider: HoverProvider = (
107107
return null;
108108
}
109109

110-
const configItem = getConfigs().items.find(
110+
const configItem = getConfigs().items.configs.find(
111111
(config) => config.name === match,
112112
);
113113

@@ -159,20 +159,24 @@ export const diagnosticProvider = (
159159
return null;
160160
}
161161

162-
const config = getConfigs().items.find(
162+
const config = getConfigs().items.configs.find(
163163
(c) => c.name === param.value,
164164
);
165165

166166
if (config) {
167167
return null;
168168
}
169169

170-
return notFound(
171-
"Config",
172-
param.value,
173-
detectedRange(param),
174-
"config",
175-
);
170+
const pathToFile = getConfigPathByName(param.value);
171+
172+
const code: NotFoundCode = pathToFile
173+
? {
174+
value: "config",
175+
target: vscode.Uri.file(projectPath(pathToFile)),
176+
}
177+
: "config";
178+
179+
return notFound("Config", param.value, detectedRange(param), code);
176180
},
177181
);
178182
};
@@ -197,7 +201,7 @@ export const completionProvider: CompletionProvider = {
197201
return [];
198202
}
199203

200-
return getConfigs().items.map((config) => {
204+
return getConfigs().items.configs.map((config) => {
201205
let completeItem = new vscode.CompletionItem(
202206
config.name,
203207
vscode.CompletionItemKind.Value,

src/features/storage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
2222
toFind,
2323
getPaths,
2424
({ param, item }) => {
25-
const configItem = getConfigs().items.find(
25+
const configItem = getConfigs().items.configs.find(
2626
(c) => c.name === `filesystems.disks.${param.value}`,
2727
);
2828

@@ -48,7 +48,7 @@ export const diagnosticProvider = (
4848
toFind,
4949
getConfigs,
5050
({ param, item, index }) => {
51-
const config = getConfigs().items.find(
51+
const config = getConfigs().items.configs.find(
5252
(c) => c.name === `filesystems.disks.${param.value}`,
5353
);
5454

@@ -83,7 +83,7 @@ export const completionProvider: CompletionProvider = {
8383
}
8484

8585
return getConfigs()
86-
.items.filter((config) => {
86+
.items.configs.filter((config) => {
8787
return (
8888
config.name.startsWith("filesystems.disks.") &&
8989
config.name.split(".").length === 3

src/features/translation.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { notFound } from "@src/diagnostic";
1+
import { notFound, NotFoundCode } from "@src/diagnostic";
22
import AutocompleteResult from "@src/parser/AutocompleteResult";
33
import {
44
getTranslationItemByName,
5+
getTranslationPathByName,
56
getTranslations,
67
TranslationItem,
78
} from "@src/repositories/translations";
89
import { config } from "@src/support/config";
910
import { findHoverMatchesInDoc } from "@src/support/doc";
1011
import { detectedRange, detectInDoc } from "@src/support/parser";
1112
import { wordMatchRegex } from "@src/support/patterns";
12-
import { relativePath } from "@src/support/project";
13+
import { projectPath, relativePath } from "@src/support/project";
1314
import { contract, createIndexMapping, facade } from "@src/support/util";
1415
import { AutocompleteParsingResult } from "@src/types";
1516
import * as vscode from "vscode";
@@ -179,22 +180,34 @@ export const diagnosticProvider = (
179180
doc,
180181
toFind,
181182
getTranslations,
182-
({ param, index }) => {
183+
({ param, index, item }) => {
183184
if (index !== 0) {
184185
return null;
185186
}
186187

187-
const item = getTranslationItemByName(param.value);
188+
const translation = getTranslationItemByName(param.value);
188189

189-
if (item) {
190+
if (translation) {
190191
return null;
191192
}
192193

194+
const pathToFile = getTranslationPathByName(
195+
param.value,
196+
getLang(item as AutocompleteParsingResult.MethodCall),
197+
);
198+
199+
const code: NotFoundCode = pathToFile
200+
? {
201+
value: "translation",
202+
target: vscode.Uri.file(projectPath(pathToFile)),
203+
}
204+
: "translation";
205+
193206
return notFound(
194207
"Translation",
195208
param.value,
196209
detectedRange(param),
197-
"translation",
210+
code,
198211
);
199212
},
200213
);

src/repositories/configs.ts

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,57 @@ import { repository } from ".";
22
import { Config } from "..";
33
import { runInLaravel, template } from "../support/php";
44

5-
export const getConfigs = repository<Config[]>({
5+
interface ConfigGroupResult {
6+
configs: Config[];
7+
paths: string[];
8+
}
9+
10+
export const getConfigPathByName = (match: string): string | undefined => {
11+
const filePath = match.replace(/\.[^.]+$/, "");
12+
13+
for (const tryPath of [
14+
filePath.replaceAll(".", "/"),
15+
filePath.replace(/^([^.]+)\..*$/, "$1"),
16+
]) {
17+
const configPath = getConfigs().items.paths.find((path) => {
18+
return (
19+
!path.startsWith("vendor/") && path.endsWith(`${tryPath}.php`)
20+
);
21+
});
22+
23+
if (configPath) {
24+
return configPath;
25+
}
26+
}
27+
};
28+
29+
export const getConfigs = repository<ConfigGroupResult>({
630
load: () => {
731
return runInLaravel<Config[]>(template("configs"), "Configs").then(
8-
(result) =>
9-
result.map((item) => {
10-
return {
11-
name: item.name,
12-
value: item.value,
13-
file: item.file,
14-
line: item.line,
15-
};
16-
}),
32+
(result) => {
33+
return {
34+
configs: result.map((item) => {
35+
return {
36+
name: item.name,
37+
value: item.value,
38+
file: item.file,
39+
line: item.line,
40+
};
41+
}),
42+
paths: [
43+
...new Set(
44+
result
45+
.filter((item) => typeof item.file === "string")
46+
.map((item) => item.file),
47+
),
48+
],
49+
} as ConfigGroupResult;
50+
},
1751
);
1852
},
1953
pattern: ["config/{,*,**/*}.php", ".env"],
20-
itemsDefault: [],
54+
itemsDefault: {
55+
configs: [],
56+
paths: [],
57+
},
2158
});

src/repositories/translations.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface TranslationGroupResult {
1818
[key: string]: TranslationItem;
1919
};
2020
languages: string[];
21+
paths: string[];
2122
}
2223

2324
interface TranslationGroupPhpResult {
@@ -66,6 +67,7 @@ const load = () => {
6667
default: res.default,
6768
translations: result,
6869
languages: res.languages,
70+
paths: res.paths,
6971
};
7072
});
7173
};
@@ -74,6 +76,16 @@ export const getTranslationItemByName = (match: string): TranslationItem | undef
7476
return getTranslations().items.translations[match.replaceAll('\\', '')];
7577
};
7678

79+
export const getTranslationPathByName = (match: string, lang: string | undefined): string | undefined => {
80+
lang = lang ?? getTranslations().items.default;
81+
82+
const fileName = match.replace(/^.*::/, '').replace(/\.[^.]+$/, '');
83+
84+
return getTranslations().items.paths.find((path) => {
85+
return !path.startsWith('vendor/') && path.endsWith(`${lang}/${fileName}.php`);
86+
});
87+
};
88+
7789
export const getTranslations = repository<TranslationGroupResult>({
7890
load,
7991
pattern: () =>
@@ -88,5 +100,6 @@ export const getTranslations = repository<TranslationGroupResult>({
88100
default: "",
89101
translations: {},
90102
languages: [],
103+
paths: [],
91104
},
92105
});

0 commit comments

Comments
 (0)