Skip to content

Commit

Permalink
Pass the view to addToOptions render functions
Browse files Browse the repository at this point in the history
FEATURE: Additional elements rendered in completion options with `addToOptions`
are now given access to the editor view.

Closes codemirror/dev#1293
  • Loading branch information
marijnh committed Nov 8, 2023
1 parent b26ff38 commit 827d851
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface CompletionConfig {
/// other added widgets and the standard content. The default icons
/// have position 20, the label position 50, and the detail position
/// 80.
addToOptions?: {render: (completion: Completion, state: EditorState) => Node | null,
addToOptions?: {render: (completion: Completion, state: EditorState, view: EditorView) => Node | null,
position: number}[]
/// By default, [info](#autocomplete.Completion.info) tooltips are
/// placed to the side of the selected completion. This option can
Expand Down
7 changes: 4 additions & 3 deletions src/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {CompletionState} from "./state"
import {completionConfig, CompletionConfig} from "./config"
import {Option, Completion, CompletionInfo, closeCompletionEffect} from "./completion"

type OptionContentSource = (completion: Completion, state: EditorState, match: readonly number[]) => Node | null
type OptionContentSource =
(completion: Completion, state: EditorState, view: EditorView, match: readonly number[]) => Node | null

function optionContent(config: Required<CompletionConfig>): OptionContentSource[] {
let content = config.addToOptions.slice() as {render: OptionContentSource, position: number}[]
Expand All @@ -20,7 +21,7 @@ function optionContent(config: Required<CompletionConfig>): OptionContentSource[
position: 20
})
content.push({
render(completion: Completion, _s: EditorState, match: readonly number[]) {
render(completion: Completion, _s: EditorState, _v: EditorView, match: readonly number[]) {
let labelElt = document.createElement("span")
labelElt.className = "cm-completionLabel"
let label = completion.displayLabel || completion.label, off = 0
Expand Down Expand Up @@ -267,7 +268,7 @@ class CompletionTooltip {
let cls = this.optionClass(completion)
if (cls) li.className = cls
for (let source of this.optionContent) {
let node = source(completion, this.view.state, match)
let node = source(completion, this.view.state, this.view, match)
if (node) li.appendChild(node)
}
}
Expand Down

0 comments on commit 827d851

Please sign in to comment.