Skip to content

Commit a283bbc

Browse files
authored
Merge pull request #252 from idris-hackers/remove-cyclejs
replace cycle js with preact
2 parents 7fdbed6 + 41c7277 commit a283bbc

File tree

10 files changed

+288
-570
lines changed

10 files changed

+288
-570
lines changed

lib/typings/cycle-core.d.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/typings/cycle-dom.d.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/utils/highlighter.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import * as CycleDOM from '@cycle/dom'
1+
import * as Preact from 'preact'
22
import Logger from './Logger'
33

4-
const highlightInfoListToOb = (list: any) => {
4+
export type HighlightInformation = {
5+
classes: Array<string>
6+
word: string
7+
description: string
8+
}
9+
10+
const highlightInfoListToOb = (list: Array<any>) => {
511
const obj: { [key: string]: any } = {}
612
for (let x of list) {
713
const key = x[0].slice(1)
@@ -13,7 +19,7 @@ const highlightInfoListToOb = (list: any) => {
1319

1420
// Use the right CSS classes, so that we can use the
1521
// syntax highlighting built into atom.
16-
const decorToClasses = (decor: any) => {
22+
const decorToClasses = (decor: string) => {
1723
switch (decor) {
1824
case ':type':
1925
return ['syntax--storage', 'syntax--type']
@@ -36,7 +42,7 @@ const decorToClasses = (decor: any) => {
3642
}
3743
}
3844

39-
const highlightWord = (word: string, info: any) => {
45+
const highlightWord = (word: string, info: any): HighlightInformation => {
4046
const type = info.info.type || ''
4147
const doc = info.info['doc-overview'] || ''
4248

@@ -51,22 +57,26 @@ const highlightWord = (word: string, info: any) => {
5157

5258
// Build highlighting information that we can then pass to one
5359
// of our serializers.
54-
export const highlight = (code: string, highlightingInfo: any) => {
60+
export const highlight = (
61+
code: string,
62+
highlightingInfo: Array<[number, number, Array<any>]>,
63+
): Array<HighlightInformation> => {
5564
const highlighted = highlightingInfo
56-
.map(function ([start, length, info]: [any, any, any]) {
65+
.map(function ([start, length, info]) {
5766
return {
5867
start,
5968
length,
6069
info: highlightInfoListToOb(info),
6170
}
6271
})
63-
.filter((i: any) => i.info.decor != null)
64-
.reduce(
65-
([position, text]: any, info: any) => {
72+
.filter((i) => i.info.decor != null)
73+
.reduce<[number, Array<HighlightInformation>]>(
74+
([position, text], info) => {
6675
const newPosition = info.start + info.length
67-
const unhighlightedText = {
76+
const unhighlightedText: HighlightInformation = {
6877
classes: [],
6978
word: code.slice(position, info.start),
79+
description: '',
7080
}
7181
const highlightedWord = highlightWord(
7282
code.slice(info.start, newPosition),
@@ -79,10 +89,11 @@ export const highlight = (code: string, highlightingInfo: any) => {
7989
[0, []],
8090
)
8191

82-
const [position, text] = Array.from(highlighted)
92+
const [position, text] = highlighted
8393
const rest = {
8494
classes: [],
8595
word: code.slice(position),
96+
description: '',
8697
}
8798
const higlightedWords = text.concat(rest)
8899
return higlightedWords.filter(
@@ -91,9 +102,9 @@ export const highlight = (code: string, highlightingInfo: any) => {
91102
}
92103

93104
// Applies the highlighting and returns the result as an html-string.
94-
export const highlightToString = (highlights: any) =>
105+
export const highlightToString = (highlights: Array<HighlightInformation>) =>
95106
highlights
96-
.map(function ({ classes, word }: any) {
107+
.map(function ({ classes, word }) {
97108
if (classes.length === 0) {
98109
return word
99110
} else {
@@ -103,8 +114,8 @@ export const highlightToString = (highlights: any) =>
103114
.join('')
104115

105116
// Applies the highlighting and returns the result as a DOM-objects.
106-
export const highlightToHtml = (highlights: any) => {
107-
const spans = highlights.map(function ({ classes, word }: any) {
117+
export const highlightToHtml = (highlights: Array<HighlightInformation>) => {
118+
const spans = highlights.map(function ({ classes, word }) {
108119
if (classes.length === 0) {
109120
return document.createTextNode(word)
110121
} else {
@@ -119,15 +130,19 @@ export const highlightToHtml = (highlights: any) => {
119130
return container
120131
}
121132

122-
export const highlightToCycle = (highlights: any) =>
123-
highlights.map(({ classes, word, description }: any) => {
133+
export const highlightToPreact = (
134+
highlights: Array<HighlightInformation>,
135+
): Preact.VNode => {
136+
const highlighted = highlights.map(({ classes, word, description }) => {
124137
if (classes.length === 0) {
125-
return word
138+
return word as string
126139
} else {
127-
return CycleDOM.h(
140+
return Preact.h(
128141
'span',
129142
{ className: classes.join(' '), title: description },
130143
word,
131144
)
132145
}
133146
})
147+
return Preact.h('div', {}, highlighted)
148+
}

lib/views/apropos-view.ts

Lines changed: 0 additions & 118 deletions
This file was deleted.

lib/views/apropos-view.tsx

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import * as Preact from 'preact'
2+
import { useState, StateUpdater } from 'preact/hooks'
3+
import * as highlighter from '../utils/highlighter'
4+
import * as Rx from 'rx-lite'
5+
import { fontOptions } from '../utils/dom'
6+
import { IdrisController } from '../idris-controller'
7+
import { IdrisModel } from '../idris-model'
8+
import { HighlightInformation } from '../utils/highlighter'
9+
10+
const styles = fontOptions()
11+
12+
const ask = (
13+
model: IdrisModel,
14+
question: string,
15+
setAnswer: StateUpdater<Array<HighlightInformation>>,
16+
) => {
17+
model
18+
.apropos(question)
19+
.map((e: any): {
20+
code: string
21+
highlightInfo: Array<[number, number, Array<any>]>
22+
} => ({
23+
code: e.msg[0],
24+
highlightInfo: e.msg[1],
25+
}))
26+
.catch((e: any) =>
27+
Rx.Observable.just({
28+
code: e.message,
29+
highlightInfo: e.highlightInformation,
30+
}),
31+
)
32+
.subscribe(
33+
({ code, highlightInfo }) => {
34+
const answer = highlighter.highlight(code, highlightInfo)
35+
setAnswer(answer)
36+
},
37+
(err) =>
38+
setAnswer([
39+
{ word: err.message, classes: [], description: '' },
40+
]),
41+
)
42+
}
43+
44+
type AnswerProps = { highlightInfo: Array<HighlightInformation> }
45+
46+
const Answer: Preact.FunctionComponent<AnswerProps> = (props) => {
47+
const { highlightInfo } = props
48+
return (
49+
<pre className="idris-apropos-output" style={styles}>
50+
{highlighter.highlightToPreact(highlightInfo)}
51+
</pre>
52+
)
53+
}
54+
55+
type AproposProps = { model: IdrisModel }
56+
57+
const Apropos: Preact.FunctionComponent<AproposProps> = (props) => {
58+
const { model } = props
59+
const [input, setInput] = useState<string>('')
60+
const [answer, setAnswer] = useState<Array<HighlightInformation>>([])
61+
62+
return (
63+
<div className="idris-panel-view">
64+
<input
65+
type="text"
66+
className="native-key-bindings idris-repl-input-field"
67+
onInput={(e) => {
68+
setInput(e.currentTarget.value)
69+
}}
70+
onKeyPress={(e) => {
71+
if (e.keyCode === 13) {
72+
ask(model, input, setAnswer)
73+
}
74+
}}
75+
>
76+
{input}
77+
</input>
78+
<div className="idris-repl-lines">
79+
<Answer highlightInfo={answer} />
80+
</div>
81+
</div>
82+
)
83+
}
84+
85+
export class AproposView {
86+
0: HTMLDivElement = document.createElement('div')
87+
88+
constructor(params: { controller: IdrisController }) {
89+
const hostElement = this[0]
90+
91+
const { model } = params.controller
92+
93+
Preact.render(<Apropos model={model} />, hostElement)
94+
}
95+
}

0 commit comments

Comments
 (0)