-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
251 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { AstNode } from '../../node/index.js' | ||
import type { CodeActionProviderContext } from '../../service/index.js' | ||
import type { LanguageError } from '../../source/index.js' | ||
|
||
export interface CodeAction { | ||
title: string | ||
command: CodeActionCommand | ||
isPreferred?: boolean | ||
errors?: LanguageError[] | ||
} | ||
|
||
export interface CodeActionCommand { | ||
id: string | ||
data?: unknown | ||
} | ||
|
||
export type CodeActionProvider<N extends AstNode = AstNode> = ( | ||
node: N, | ||
ctx: CodeActionProviderContext, | ||
) => readonly CodeAction[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { AstNode } from '../../node/index.js' | ||
import { FileNode } from '../../node/index.js' | ||
import type { MetaRegistry } from '../../service/index.js' | ||
import { Range } from '../../source/index.js' | ||
import { traversePreOrder } from '../util.js' | ||
import type { CodeAction, CodeActionProvider } from './CodeAction.js' | ||
|
||
export const fallback: CodeActionProvider<AstNode> = (node, ctx) => { | ||
const ans: CodeAction[] = [] | ||
traversePreOrder( | ||
node, | ||
(node) => Range.containsRange(node.range, ctx.range, true), | ||
(node) => ctx.meta.hasCodeActionProvider(node.type), | ||
(node) => ans.push(...ctx.meta.getCodeActionProvider(node.type)(node, ctx)), | ||
) | ||
return ans | ||
} | ||
|
||
export const file: CodeActionProvider<FileNode<AstNode>> = (node, ctx) => { | ||
const ans: CodeAction[] = [] | ||
for (const error of FileNode.getErrors(node)) { | ||
const action = error.info?.codeAction | ||
if (!action) { | ||
continue | ||
} | ||
if (!Range.containsRange(error.range, ctx.range, true)) { | ||
continue | ||
} | ||
ans.push({ | ||
title: action.title, | ||
isPreferred: action.isPreferred, | ||
errors: [error], | ||
command: action.command, | ||
}) | ||
} | ||
return ans | ||
} | ||
|
||
export function registerProviders(meta: MetaRegistry) { | ||
meta.registerCodeActionProvider<FileNode<AstNode>>('file', file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * as codeActions from './builtin.js' | ||
export * from './CodeAction.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import type { CommandContext } from '../service/index.js' | ||
|
||
export type Command = (options: unknown, ctx: CommandContext) => void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.