11import {
2- Completion ,
3- insertCompletionText ,
4- pickedCompletion ,
2+ type Completion ,
3+ insertCompletionText ,
4+ pickedCompletion ,
55} from "@codemirror/autocomplete" ;
6- import { RawCompletion , RawCompletionItem } from "../types.js" ;
7- import { TransactionSpec } from "@codemirror/state" ;
6+ import type { TransactionSpec } from "@codemirror/state" ;
7+ import type { EditorView } from "codemirror" ;
8+ import type ts from "typescript" ;
89import { renderDisplayParts } from "../hover/renderTooltip.js" ;
9- import { EditorView } from "codemirror" ;
10- import ts from "typescript" ;
10+ import type { RawCompletion , RawCompletionItem } from "../types.js" ;
1111
1212export function deserializeCompletions ( raw : RawCompletion | null ) {
13- if ( ! raw ) return raw ;
14- return {
15- from : raw . from ,
16- options : raw . options . map ( ( o ) => deserializeCompletion ( o ) ) ,
17- } ;
13+ if ( ! raw ) return raw ;
14+ return {
15+ from : raw . from ,
16+ options : raw . options . map ( ( o ) => deserializeCompletion ( o ) ) ,
17+ } ;
1818}
1919
2020function deserializeCompletion ( raw : RawCompletionItem ) : Completion {
21- const { codeActions, label, type } = raw ;
21+ const { codeActions, label, type } = raw ;
2222
23- return {
24- label,
25- type,
26- apply : codeActions ? codeActionToApplyFunction ( codeActions ) : raw . label ,
27- info : ( ) => {
28- const elem = document . createElement ( "div" ) ;
29- elem . appendChild ( renderDisplayParts ( raw . displayParts ) ) ;
30- return elem ;
31- } ,
32- } ;
23+ return {
24+ label,
25+ type,
26+ apply : codeActions ? codeActionToApplyFunction ( codeActions ) : raw . label ,
27+ info : ( ) => {
28+ const elem = document . createElement ( "div" ) ;
29+ elem . appendChild ( renderDisplayParts ( raw . displayParts ) ) ;
30+ return elem ;
31+ } ,
32+ } ;
3333}
3434
3535/**
@@ -40,48 +40,48 @@ function deserializeCompletion(raw: RawCompletionItem): Completion {
4040 * lets you import them automatically.
4141 */
4242export function codeActionToApplyFunction ( codeActions : ts . CodeAction [ ] ) {
43- return (
44- view : EditorView ,
45- completion : Completion ,
46- from : number ,
47- to : number ,
48- ) => {
49- const insTransaction : TransactionSpec = {
50- ...insertCompletionText ( view . state , completion . label , from , to ) ,
51- annotations : pickedCompletion . of ( completion ) ,
52- } ;
43+ return (
44+ view : EditorView ,
45+ completion : Completion ,
46+ from : number ,
47+ to : number ,
48+ ) => {
49+ const insTransaction : TransactionSpec = {
50+ ...insertCompletionText ( view . state , completion . label , from , to ) ,
51+ annotations : pickedCompletion . of ( completion ) ,
52+ } ;
5353
54- const actionTransactions : TransactionSpec [ ] = [ ] ;
54+ const actionTransactions : TransactionSpec [ ] = [ ] ;
5555
56- // Complete, but also implement code actions.
57- // https://github.com/codemirror/autocomplete/blob/30307656e85c9e5911a69fe2432de05be1580958/src/state.ts#L322
58- for ( const action of codeActions ) {
59- for ( const change of action . changes ) {
60- for ( const textChange of change . textChanges ) {
61- // Note that this may be dangerous! We've had many problems
62- // with trying to dispatch transactions on CodeMirror when the length
63- // of the document is different than what it expects or needs. I think
64- // that this will be safe in that case because we're combining
65- // and only declaring the length once.
66- //
67- // NOTE: this has less than ideal history behavior! ideal this would
68- // be composed with `insTransaction` and produce one history event.
69- // But that is tough because the two need to perfectly agree on the document that
70- // they're editing, and the length of the document changes.
71- actionTransactions . push ( {
72- changes : [
73- {
74- from : textChange . span . start ,
75- to : textChange . span . start + textChange . span . length ,
76- insert : textChange . newText ,
77- } ,
78- ] ,
79- annotations : pickedCompletion . of ( completion ) ,
80- } ) ;
81- }
82- }
83- }
56+ // Complete, but also implement code actions.
57+ // https://github.com/codemirror/autocomplete/blob/30307656e85c9e5911a69fe2432de05be1580958/src/state.ts#L322
58+ for ( const action of codeActions ) {
59+ for ( const change of action . changes ) {
60+ for ( const textChange of change . textChanges ) {
61+ // Note that this may be dangerous! We've had many problems
62+ // with trying to dispatch transactions on CodeMirror when the length
63+ // of the document is different than what it expects or needs. I think
64+ // that this will be safe in that case because we're combining
65+ // and only declaring the length once.
66+ //
67+ // NOTE: this has less than ideal history behavior! ideal this would
68+ // be composed with `insTransaction` and produce one history event.
69+ // But that is tough because the two need to perfectly agree on the document that
70+ // they're editing, and the length of the document changes.
71+ actionTransactions . push ( {
72+ changes : [
73+ {
74+ from : textChange . span . start ,
75+ to : textChange . span . start + textChange . span . length ,
76+ insert : textChange . newText ,
77+ } ,
78+ ] ,
79+ annotations : pickedCompletion . of ( completion ) ,
80+ } ) ;
81+ }
82+ }
83+ }
8484
85- view . dispatch ( ...[ insTransaction , ...actionTransactions ] ) ;
86- } ;
85+ view . dispatch ( ...[ insTransaction , ...actionTransactions ] ) ;
86+ } ;
8787}
0 commit comments