Skip to content
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

support D-Scanner autofixes in light bulbs #326

Merged
merged 2 commits into from
Jul 9, 2023
Merged
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
4 changes: 2 additions & 2 deletions dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dfmt": "0.15.0",
"diet-complete": "0.0.3",
"diet-ng": "1.8.1",
"dscanner": "0.15.0",
"dscanner": "0.16.0-beta.2",
"dub": "1.33.1",
"emsi_containers": "0.9.0",
"eventcore": "0.9.25",
Expand All @@ -18,7 +18,7 @@
"isfreedesktop": "0.1.1",
"libasync": "0.8.6",
"libddoc": "0.8.0",
"libdparse": "0.23.1",
"libdparse": "0.23.2",
"memutils": "1.0.9",
"mir-algorithm": "3.20.4",
"mir-core": "1.5.5",
Expand Down
92 changes: 88 additions & 4 deletions source/served/commands/code_actions.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ CodeAction[] provideCodeActions(CodeActionParams params)
auto instance = activeInstance = backend.getBestInstance(document.uri.uriToFile);
if (document.getLanguageId != "d" || !instance)
return [];
auto config = workspace(params.textDocument.uri).config;

// eagerly load DCD in opened files which request code actions
if (instance.has!DCDComponent)
Expand Down Expand Up @@ -89,7 +90,7 @@ CodeAction[] provideCodeActions(CodeActionParams params)
{
if (diagnostic.source == DScannerDiagnosticSource)
{
addDScannerDiagnostics(ret, instance, document, diagnostic, params);
addDScannerDiagnostics(config, ret, instance, document, diagnostic, params);
}
else if (diagnostic.source == SyntaxHintDiagnosticSource)
{
Expand Down Expand Up @@ -218,21 +219,104 @@ void addDubDiagnostics(ref CodeAction[] ret, WorkspaceD.Instance instance,
}
}

void addDScannerDiagnostics(ref CodeAction[] ret, WorkspaceD.Instance instance,
void addDScannerDiagnostics(const ref UserConfiguration config,
ref CodeAction[] ret, WorkspaceD.Instance instance,
Document document, Diagnostic diagnostic, CodeActionParams params)
{
import dscanner.analysis.imports_sortedness : ImportSortednessCheck;
import served.linters.dscanner : diagnosticDataToCodeReplacement,
diagnosticDataToResolveContext, servedDefaultDscannerConfig;
import workspaced.com.dscanner : DScannerAutoFix;

string key = diagnostic.code.orDefault.match!((string s) => s, _ => cast(string)(null));
Position cachePos;
size_t cacheIndex;

TextEdit toTextEdit(DScannerAutoFix.CodeReplacement replacement)
{
return TextEdit(
TextRange(
document.nextPositionBytes(cachePos, cacheIndex, replacement.range[0]),
document.nextPositionBytes(cachePos, cacheIndex, replacement.range[1])
),
replacement.newText
);
}

info("Diagnostic: ", diagnostic);
string key = diagnostic.code.orDefault.match!((string s) => s, _ => cast(string)(null));

if (key == ImportSortednessCheck.KEY)
{
ret ~= CodeAction(Command("Sort imports", "code-d.sortImports",
[JsonValue(document.positionToOffset(params.range[0]))]));
}

if (!diagnostic.data.isNone)
{
auto data = diagnostic.data.deref;
if (auto autofixes = "autofixes" in data.object)
{
string checkName = data.object["checkName"].string;

DScannerAutoFix.ResolveContext[] resolveContexts;
size_t[] resolveContextIndices;

foreach (autofix; autofixes.array)
{
if (autofix.kind != JsonValue.Kind.object)
{
warning("Unsupported dscanner autofix: ", autofix);
continue;
}

auto nameJson = "name" in autofix.object;
if (!nameJson)
{
warning("Unsupported dscanner autofix: ", autofix);
continue;
}
string name = (*nameJson).get!string;

DScannerAutoFix.CodeReplacement[] codeReplacements;

if (auto replacements = "replacements" in autofix.object)
{
codeReplacements = (*replacements).array.map!(
j => diagnosticDataToCodeReplacement(j)
).array;
}
else if (auto jcontext = "context" in autofix.object)
{
resolveContexts ~= diagnosticDataToResolveContext(*jcontext);
resolveContextIndices ~= ret.length;
}
else
{
warning("Unsupported dscanner autofix: ", autofix);
continue;
}

ret ~= CodeAction(name, WorkspaceEdit([
document.uri: codeReplacements.map!toTextEdit.array
]));
}

if (resolveContexts.length)
{
auto codeReplacementsList = instance.get!DscannerComponent.resolveAutoFixes(
checkName, resolveContexts, document.uri.uriToFile, "dscanner.ini",
generateDfmtArgs(config, document.eolAt(0)), document.rawText, false,
servedDefaultDscannerConfig).getYield;

foreach (i, codeReplacements; codeReplacementsList)
{
ret[resolveContextIndices[i]].edit = WorkspaceEdit([
document.uri: codeReplacements.map!toTextEdit.array
]);
}
}
}
}

if (key.length)
{
JsonValue code = diagnostic.code.match!(() => JsonValue(null), j => j);
Expand Down
66 changes: 66 additions & 0 deletions source/served/linters/dscanner.d
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,72 @@ void convertFromWorkspaced(ref Diagnostic d, Document document, DScannerIssue is
/* message: */ suppl.description
)
).array;
if (issue.autofixes.length)
d.data = JsonValue([
"checkName": JsonValue(issue.checkName),
"autofixes": JsonValue(issue.autofixes.map!toJson.array)
]);
}

private JsonValue toJson(DScannerAutoFix f)
{
import std.array : array;

JsonValue[string] ret = [
"name": JsonValue(f.name)
];
f.replacements.match!(
(DScannerAutoFix.CodeReplacement[] replacements) {
ret["replacements"] = JsonValue(replacements.map!(a => toJson(a)).array);
},
(DScannerAutoFix.ResolveContext context) {
ret["context"] = toJson(context);
}
);
return JsonValue(ret);
}

private JsonValue toJson(DScannerAutoFix.CodeReplacement replacement)
{
return JsonValue([
"r": JsonValue([JsonValue(replacement.range[0]), JsonValue(replacement.range[1])]),
"t": JsonValue(replacement.newText)
]);
}

DScannerAutoFix.CodeReplacement diagnosticDataToCodeReplacement(JsonValue json)
{
return DScannerAutoFix.CodeReplacement(
[
json.object["r"].array[0].integer,
json.object["r"].array[1].integer
],
json.object["t"].string
);
}

private JsonValue toJson(DScannerAutoFix.ResolveContext context)
{
auto params = new JsonValue[context.params.length];
foreach (i, param; context.params)
params[i] = JsonValue(param);
return JsonValue([
"p": JsonValue(params),
"s": JsonValue(context.extraInfo)
]);
}

DScannerAutoFix.ResolveContext diagnosticDataToResolveContext(JsonValue json)
{
size_t i;
return DScannerAutoFix.ResolveContext(
[
json.object["p"].array[i++].integer,
json.object["p"].array[i++].integer,
json.object["p"].array[i++].integer,
],
json.object["s"].string
);
}

/// Sets the range for the diagnostic from the issue
Expand Down
3 changes: 2 additions & 1 deletion test/tc_dub/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ void main()
try
{
auto result = dub.build.getBlocking;
assert(result.count!(a => a.type == ErrorType.Warning || a.type == ErrorType.Error) == 0);
assert(result.count!(a => a.type == ErrorType.Warning || a.type == ErrorType.Error) == 0,
"got unexpected warnings/errors: " ~ result.to!string);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion workspace-d/dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependency "inifiled" version="1.3.3"
dependency "serve-d:dcd" path=".."
dependency "dub" version="1.33.1"
dependency "emsi_containers" version="0.9.0"
dependency "dscanner" version="0.15.0"
dependency "dscanner" version="~>0.16.0-beta.1"
dependency "libdparse" version="~>0.23.0"
dependency "standardpaths" version="0.8.2"
dependency "mir-algorithm" version="~>3.20"
Expand Down
4 changes: 2 additions & 2 deletions workspace-d/dub.selections.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"versions": {
"dcd": "0.16.0-beta.2",
"dfmt": "0.15.0",
"dscanner": "0.15.0",
"dscanner": "0.16.0-beta.2",
"dub": "1.33.1",
"emsi_containers": "0.9.0",
"inifiled": "1.3.3",
"isfreedesktop": "0.1.1",
"libddoc": "0.8.0",
"libdparse": "0.23.1",
"libdparse": "0.23.2",
"mir-algorithm": "3.20.4",
"mir-core": "1.5.5",
"msgpack-d": "1.0.4",
Expand Down
18 changes: 12 additions & 6 deletions workspace-d/source/workspaced/com/dfmt.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ class DfmtComponent : ComponentWrapper
if (!code.strip.length)
return null;

Config config = parseConfig(arguments);
auto output = appender!string;
fmt("stdin", cast(ubyte[]) code, output, &config);
if (output.data.length)
return output.data;
else
return code.idup;
}

static Config parseConfig(string[] arguments)
{
Config config;
config.initializeWithDefaults();
string configPath;
Expand Down Expand Up @@ -143,12 +154,7 @@ class DfmtComponent : ComponentWrapper
);
//dfmt on
}
auto output = appender!string;
fmt("stdin", cast(ubyte[]) code, output, &config);
if (output.data.length)
return output.data;
else
return code.idup;
return config;
}

/// Finds dfmt instruction comments (dfmt off, dfmt on)
Expand Down
Loading