Skip to content

Commit

Permalink
Surface fixes in the playground
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Mar 26, 2024
1 parent 0b53e3d commit b338760
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions website/src/components/PlaygroundFeatures/linter.ts
Expand Up @@ -2,6 +2,7 @@ import { createSystem, createVirtualCompilerHost } from "@typescript/vfs";
import * as ts from "typescript";
import { buildSchemaAndDocResultWithHost } from "grats/src/lib";
import { codegen } from "grats/src/codegen";
import { ReportableDiagnostics } from "grats/src/utils/DiagnosticError";
import { printSDLWithoutMetadata } from "grats/src/printSchema";
import { linter } from "@codemirror/lint";
import { DocumentNode, GraphQLSchema, print } from "graphql";
Expand Down Expand Up @@ -56,7 +57,10 @@ function buildSchemaResultWithFsMap(fsMap, text: string, config) {
const message = `Grats playground bug encountered. Please report this error:\n\n ${e.stack}`;
return {
kind: "ERROR",
err: { formatDiagnosticsWithContext: () => message, _diagnostics: [] },
err: {
formatDiagnosticsWithContext: () => message,
_diagnostics: [] as ReportableDiagnostics[],
},
};
}
}
Expand All @@ -81,12 +85,17 @@ export function createLinter(fsMap, view, config) {
});

return result.err._diagnostics.map((diagnostic) => {
const actions = [];
if (diagnostic.fix) {
const action = gratsFixToCodeMirrorAction(diagnostic.fix);
actions.push(action);
}
return {
from: diagnostic.start,
to: diagnostic.start + diagnostic.length,
severity: "error",
message: diagnostic.messageText,
actions: [],
actions,
};
});
}
Expand Down Expand Up @@ -124,3 +133,24 @@ function commentLines(text: string): string {
.map((line) => `# ${line}`)
.join("\n");
}

function gratsFixToCodeMirrorAction(fix) {
const change = fix.changes[0]?.textChanges[0];
if (change == null) {
return null;
}
return {
name: fix.description,
apply: (view) => {
view.dispatch({
changes: [
{
from: change.span.start,
to: change.span.start + change.span.length,
insert: change.newText,
},
],
});
},
};
}

0 comments on commit b338760

Please sign in to comment.