captbaritone / grats

Implementation-First GraphQL for TypeScript
https://grats.capt.dev
MIT License
309 stars 16 forks source link

TS Plugin Feature Ideas #115

Open captbaritone opened 10 months ago

captbaritone commented 10 months ago
captbaritone commented 7 months ago

Sketch of inlay hints

    proxy.provideInlayHints = (fileName, range, context) => {
      const prior = info.languageService.provideInlayHints(
        fileName,
        range,
        context,
      );

      const doc = info.languageService.getProgram()?.getSourceFile(fileName);

      if (doc == null) return prior;
      const result = extract(doc);

      if (result.kind === "ERROR") return prior;

      const hints = result.value.definitions
        .map((def) => {
          if (
            def.kind === Kind.OBJECT_TYPE_DEFINITION &&
            def.name.loc != null
          ) {
            return {
              text: `${def.name.value}`,
              position: def.name.loc.end,
              kind: ts.InlayHintKind.Type,
            };
          }
        })
        .filter((hint): hint is TS.InlayHint => hint != null);

      return [...prior, ...hints];
    };
captbaritone commented 7 months ago

Sketch of code actions

   proxy.getCodeFixesAtPosition = (
      fileName: string,
      start: number,
      end: number,
      errorCodes: readonly number[],
      formatOptions: TS.FormatCodeSettings,
      preferences: TS.UserPreferences,
    ): readonly TS.CodeFixAction[] => {
      const prior = info.languageService.getCodeFixesAtPosition(
        fileName,
        start,
        end,
        errorCodes,
        formatOptions,
        preferences,
      );
      const doc = info.languageService.getProgram()?.getSourceFile(fileName);

      if (doc == null) return prior;
      const result = extract(doc);

      if (result.kind === "OK") return prior;

      const relatedDiagnostics = result.err.filter((err) => {
        return (
          err.fix != null &&
          err.start === start &&
          err.length === end - start &&
          err.file.fileName === doc.fileName &&
          errorCodes.includes(err.code)
        );
      });

      const fixes = relatedDiagnostics.map((err) => {
        return err.fix!;
      });

      return [...prior, ...fixes];
    };

Note: Need to define getSupportedCodeFixes

https://www.threads.net/@captbaritone/post/C49lzTfOkEt