Skip to content

Addon API

Alex Kanunnikov edited this page May 11, 2020 · 3 revisions

RFC issue

Addon API interface

import { Definition, FileChangeType, Diagnostic, TextDocument, Location, TextDocumentIdentifier, Position, CompletionItem, CodeActionParams } from 'vscode-languageserver';
import Server from '../server';
import ASTPath from './../glimmer-utils';

type Executor = (server: Server, command: string, args: any[]) => any;
type Linter = (document: TextDocument) => Diagnostic[];
type Watcher = (uri: string, change: FileChangeType) => any;

type MatchResultType =
  | 'helper'
  | 'service'
  | 'route'
  | 'controller'
  | 'modifier'
  | 'template'
  | 'component'
  | 'model'
  | 'transform'
  | 'adapter'
  | 'serializer';

interface MatchResult {
  type: MatchResultType;
  name: string;
}

interface Project {
  root: string; // project entry path
  addCommandExecutor(key: string, fn: Executor): void;
  addLinter(fn: Linter): void;
  addWatcher(fn: Watcher): void;
  matchPathToType(filePath: string): null | MatchResult;
}

interface Registry {
  component: {
    [componentName: string]: string[] // files, related to component
  },
  service: {
    [serviceName: string]: string[] // files, related to service
  },
  routePath: {
    [routePath: string]: string[] // files, related to route (templates, controllers, routes)
  }
}

interface Command {
   command: string // els.executeInEmberCLI
   arguments: any[] // first argument - file path / project root, if command scoped to project
}
interface Server {
  getRegistry(projectRoot: string): Registry;
  onExecute(command: Command): any;
  getUsages(normalizedName: string): string[]; // return list of files, related to token
}

interface BaseAPIParams {
  server: Server;
  textDocument: TextDocumentIdentifier;
  position: Position;
}
interface ExtendedAPIParams {
  focusPath: ASTPath;
  type: 'script' | 'template';
}
interface ReferenceFunctionParams extends BaseAPIParams {
  results: Location[];
}
interface CompletionFunctionParams extends ExtendedAPIParams {
  results: CompletionItem[];
}
interface DefinitionFunctionParams extends ExtendedAPIParams {
  results: Location[];
}

interface CodeActionFunctionParams extends CodeActionParams {
  results: (Command | CodeAction)[];
  server: Server;
  document: TextDocument
}

type ReferenceResolveFunction = (root: string, params: ReferenceFunctionParams) => Promise<Location[]>;
type CompletionResolveFunction = (root: string, params: CompletionFunctionParams) => Promise<CompletionItem[]>;
type DefinitionResolveFunction = (root: string, params: DefinitionFunctionParams) => Promise<Location[]>;
type CodeActionResolveFunction = (root: string, params: CodeActionParams) => Promise<(Command | CodeAction)[] | undefined | null>;
type InitCallback = (server: Server, project: Project) => void;


export interface AddonAPI {
  onReference: undefined | ReferenceResolveFunction;
  onComplete: undefined | CompletionResolveFunction;
  onDefinition: undefined | DefinitionResolveFunction;
  onCodeAction: undefined | CodeActionResolveFunction;
  onInit: undefined | InitCallback;
}
Clone this wiki locally