Skip to content

Support for linked parameter vscode.Diagnostic.code for diagnosticProvider #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/diagnostic/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Diagnostic, DiagnosticSeverity, Range } from "vscode";
import { Diagnostic, DiagnosticSeverity, Range, Uri } from "vscode";

export const notFound = (
descriptor: string,
match: string,
range: Range,
code: DiagnosticCode,
code: DiagnosticCode | DiagnosticCodeObject,
): Diagnostic => ({
message: `${descriptor} [${match}] not found.`,
severity: DiagnosticSeverity.Warning,
Expand All @@ -13,6 +13,11 @@ export const notFound = (
code,
});

export type DiagnosticCodeObject = {
value: DiagnosticCode;
target: Uri;
};

export type DiagnosticCode =
| "appBinding"
| "asset"
Expand Down
21 changes: 14 additions & 7 deletions src/features/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { notFound } from "@src/diagnostic";
import { DiagnosticCodeObject, notFound } from "@src/diagnostic";
import AutocompleteResult from "@src/parser/AutocompleteResult";
import { getConfigs } from "@src/repositories/configs";
import { getConfigPathByName, getConfigs } from "@src/repositories/configs";
import { config } from "@src/support/config";
import { findHoverMatchesInDoc } from "@src/support/doc";
import { detectedRange, detectInDoc } from "@src/support/parser";
Expand Down Expand Up @@ -75,7 +75,7 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
return null;
}

const configItem = getConfigs().items.find(
const configItem = getConfigs().items.configs.find(
(config) => config.name === param.value,
);

Expand Down Expand Up @@ -107,7 +107,7 @@ export const hoverProvider: HoverProvider = (
return null;
}

const configItem = getConfigs().items.find(
const configItem = getConfigs().items.configs.find(
(config) => config.name === match,
);

Expand Down Expand Up @@ -159,19 +159,26 @@ export const diagnosticProvider = (
return null;
}

const config = getConfigs().items.find(
const config = getConfigs().items.configs.find(
(c) => c.name === param.value,
);

if (config) {
return null;
}

const pathToFile = getConfigPathByName(param.value);

const code = pathToFile ? {
value: "config",
target: vscode.Uri.file(projectPath(pathToFile)),
} as DiagnosticCodeObject : "config";

return notFound(
"Config",
param.value,
detectedRange(param),
"config",
code,
);
},
);
Expand All @@ -197,7 +204,7 @@ export const completionProvider: CompletionProvider = {
return [];
}

return getConfigs().items.map((config) => {
return getConfigs().items.configs.map((config) => {
let completeItem = new vscode.CompletionItem(
config.name,
vscode.CompletionItemKind.Value,
Expand Down
6 changes: 3 additions & 3 deletions src/features/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const linkProvider: LinkProvider = (doc: vscode.TextDocument) => {
toFind,
getPaths,
({ param, item }) => {
const configItem = getConfigs().items.find(
const configItem = getConfigs().items.configs.find(
(c) => c.name === `filesystems.disks.${param.value}`,
);

Expand All @@ -48,7 +48,7 @@ export const diagnosticProvider = (
toFind,
getConfigs,
({ param, item, index }) => {
const config = getConfigs().items.find(
const config = getConfigs().items.configs.find(
(c) => c.name === `filesystems.disks.${param.value}`,
);

Expand Down Expand Up @@ -83,7 +83,7 @@ export const completionProvider: CompletionProvider = {
}

return getConfigs()
.items.filter((config) => {
.items.configs.filter((config) => {
return (
config.name.startsWith("filesystems.disks.") &&
config.name.split(".").length === 3
Expand Down
23 changes: 17 additions & 6 deletions src/features/translation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { notFound } from "@src/diagnostic";
import { DiagnosticCodeObject, notFound } from "@src/diagnostic";
import AutocompleteResult from "@src/parser/AutocompleteResult";
import {
getTranslationItemByName,
getTranslationPathByName,
getTranslations,
TranslationItem,
} from "@src/repositories/translations";
import { config } from "@src/support/config";
import { findHoverMatchesInDoc } from "@src/support/doc";
import { detectedRange, detectInDoc } from "@src/support/parser";
import { wordMatchRegex } from "@src/support/patterns";
import { relativePath } from "@src/support/project";
import { projectPath, relativePath } from "@src/support/project";
import { contract, createIndexMapping, facade } from "@src/support/util";
import { AutocompleteParsingResult } from "@src/types";
import * as vscode from "vscode";
Expand Down Expand Up @@ -179,22 +180,32 @@ export const diagnosticProvider = (
doc,
toFind,
getTranslations,
({ param, index }) => {
({ param, index, item }) => {
if (index !== 0) {
return null;
}

const item = getTranslationItemByName(param.value);
const translation = getTranslationItemByName(param.value);

if (item) {
if (translation) {
return null;
}

const pathToFile = getTranslationPathByName(
param.value,
getLang(item as AutocompleteParsingResult.MethodCall)
);

const code = pathToFile ? {
value: "translation",
target: vscode.Uri.file(projectPath(pathToFile)),
} as DiagnosticCodeObject : "translation";

return notFound(
"Translation",
param.value,
detectedRange(param),
"translation",
code,
);
},
);
Expand Down
42 changes: 31 additions & 11 deletions src/repositories/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@ import { repository } from ".";
import { Config } from "..";
import { runInLaravel, template } from "../support/php";

export const getConfigs = repository<Config[]>({
interface ConfigGroupResult {
configs: Config[];
paths: string[];
}

export const getConfigPathByName = (match: string): string | undefined => {
const fileName = match.replace(/\.[^.]+$/, '');

return getConfigs().items.paths.find((path) => {
return !path.startsWith('vendor/') && path.endsWith(`${fileName}.php`);
});
};

export const getConfigs = repository<ConfigGroupResult>({
load: () => {
return runInLaravel<Config[]>(template("configs"), "Configs").then(
(result) =>
result.map((item) => {
return {
name: item.name,
value: item.value,
file: item.file,
line: item.line,
};
}),
(result) => {
return {
configs: result.map((item) => {
return {
name: item.name,
value: item.value,
file: item.file,
line: item.line,
};
}),
paths: [...new Set(result.map(item => item.file))]
} as ConfigGroupResult;
}
);
},
pattern: ["config/{,*,**/*}.php", ".env"],
itemsDefault: [],
itemsDefault: {
configs: [],
paths: [],
},
});
13 changes: 13 additions & 0 deletions src/repositories/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface TranslationGroupResult {
[key: string]: TranslationItem;
};
languages: string[];
paths: string[];
}

interface TranslationGroupPhpResult {
Expand Down Expand Up @@ -66,6 +67,7 @@ const load = () => {
default: res.default,
translations: result,
languages: res.languages,
paths: res.paths,
};
});
};
Expand All @@ -74,6 +76,16 @@ export const getTranslationItemByName = (match: string): TranslationItem | undef
return getTranslations().items.translations[match.replaceAll('\\', '')];
};

export const getTranslationPathByName = (match: string, lang: string | undefined): string | undefined => {
lang = lang ?? getTranslations().items.default;

const fileName = match.replace(/^.*::/, '').replace(/\.[^.]+$/, '');

return getTranslations().items.paths.find((path) => {
return !path.startsWith('vendor/') && path.endsWith(`${lang}/${fileName}.php`);
});
};

export const getTranslations = repository<TranslationGroupResult>({
load,
pattern: () =>
Expand All @@ -88,5 +100,6 @@ export const getTranslations = repository<TranslationGroupResult>({
default: "",
translations: {},
languages: [],
paths: [],
},
});