Fix builtins syntax highlighting
This commit is contained in:
		
							
								
								
									
										19
									
								
								codetab.ts
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								codetab.ts
									
									
									
									
									
								
							| @@ -5,7 +5,7 @@ import { COLOR } from "./colors.ts"; | |||||||
| import { getCodeSheet, setSheet } from "./sheet.ts"; | import { getCodeSheet, setSheet } from "./sheet.ts"; | ||||||
| import { K, ctrlKeyDown, getKeyboardString, keyPressed, shiftKeyDown } from "./keyboard.ts"; | import { K, ctrlKeyDown, getKeyboardString, keyPressed, shiftKeyDown } from "./keyboard.ts"; | ||||||
| import { clipboard, tokenize } from "./deps.ts"; | import { clipboard, tokenize } from "./deps.ts"; | ||||||
| import { getContext } from "./runcode.ts"; | import { getBuiltins } from "./runcode.ts"; | ||||||
| import { page } from "./viewsheets.ts"; | import { page } from "./viewsheets.ts"; | ||||||
|  |  | ||||||
| const historyDebounceFrames = 20; | const historyDebounceFrames = 20; | ||||||
| @@ -39,9 +39,11 @@ const state = { | |||||||
| 		if (this.historyIndex === this.history.length && this.historyDebounce > 0) { | 		if (this.historyIndex === this.history.length && this.historyDebounce > 0) { | ||||||
| 			this.snapshot(); | 			this.snapshot(); | ||||||
| 		} | 		} | ||||||
|  | 		console.log('historyIndex', this.historyIndex); | ||||||
| 		if (this.historyIndex > 0) { | 		if (this.historyIndex > 0) { | ||||||
| 			this.historyIndex -= 1; | 			this.historyIndex -= 1; | ||||||
| 			const snap = this.history[this.historyIndex]; | 			const snap = this.history[this.historyIndex]; | ||||||
|  | 			console.log('historyIndex', this.historyIndex); | ||||||
| 			this.code = snap.code; | 			this.code = snap.code; | ||||||
| 			this.setSelection(snap.anchor, snap.focus); | 			this.setSelection(snap.anchor, snap.focus); | ||||||
| 		} | 		} | ||||||
| @@ -437,18 +439,16 @@ const drawCodeField = (code: string, x: number, y: number, w: number, h: number) | |||||||
| 		focus, | 		focus, | ||||||
| 	} = state; | 	} = state; | ||||||
| 	fillRect(x, y, w, h, COLOR.DARKERBLUE); | 	fillRect(x, y, w, h, COLOR.DARKERBLUE); | ||||||
| 	if (anchor === focus) { | 	if (anchor !== focus) { | ||||||
| 		const rect = indexToRect(code, focus); |  | ||||||
| 		fillRect(x+rect.x-scrollX, y+rect.y-scrollY, 1, rect.h+1, COLOR.YELLOW); |  | ||||||
| 	} else { |  | ||||||
| 		for (let i = Math.min(anchor, focus); i < Math.max(anchor, focus); i++) { | 		for (let i = Math.min(anchor, focus); i < Math.max(anchor, focus); i++) { | ||||||
| 			const sel = indexToRect(code, i); | 			const sel = indexToRect(code, i); | ||||||
| 			fillRect(x+sel.x-scrollX, y+sel.y-scrollY, sel.w+2, sel.h+1, COLOR.WHITE); | 			fillRect(x+sel.x-scrollX, y+sel.y-scrollY, sel.w+2, sel.h, COLOR.WHITE); | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 	const rect = indexToRect(code, focus); | 	const rect = indexToRect(code, focus); | ||||||
| 		fillRect(x+rect.x-scrollX, y+rect.y-scrollY, 1, rect.h+1, COLOR.YELLOW); | 	fillRect(x+rect.x-scrollX, y+rect.y-scrollY, 1, rect.h, COLOR.YELLOW); | ||||||
| 	} |  | ||||||
| 	const builtins = Object.keys(getContext()); | 	const builtins = Object.keys(getBuiltins()); | ||||||
| 	const tokens = [...tokenize(code)]; | 	const tokens = [...tokenize(code)]; | ||||||
| 	let cx = 0; | 	let cx = 0; | ||||||
| 	let cy = 0; | 	let cy = 0; | ||||||
| @@ -554,7 +554,6 @@ const update = async () => { | |||||||
| 	} | 	} | ||||||
| 	if (keyPressed(K.ARROW_UP)) { | 	if (keyPressed(K.ARROW_UP)) { | ||||||
| 		const rect = indexToRect(state.code, focus); | 		const rect = indexToRect(state.code, focus); | ||||||
| 		console.log(rect, focus, pixelToIndex(state.code, rect.x, rect.y-1)); |  | ||||||
| 		const newIndex = pixelToIndex(state.code, rect.x, rect.y-1-1); | 		const newIndex = pixelToIndex(state.code, rect.x, rect.y-1-1); | ||||||
| 		if (shiftKeyDown()) { | 		if (shiftKeyDown()) { | ||||||
| 			state.setFocus(newIndex); | 			state.setFocus(newIndex); | ||||||
|   | |||||||
							
								
								
									
										11
									
								
								runcode.ts
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								runcode.ts
									
									
									
									
									
								
							| @@ -2,8 +2,13 @@ | |||||||
| const G: any = { | const G: any = { | ||||||
| 	eval: eval, | 	eval: eval, | ||||||
| }; | }; | ||||||
|  | // deno-lint-ignore no-explicit-any | ||||||
|  | const builtins: any = {}; | ||||||
| const context = new Proxy(G, { | const context = new Proxy(G, { | ||||||
| 	get: (target, prop) => { | 	get: (target, prop) => { | ||||||
|  | 		if (prop in builtins) { | ||||||
|  | 			return builtins[prop as keyof typeof builtins]; | ||||||
|  | 		} | ||||||
| 		return target[prop]; | 		return target[prop]; | ||||||
| 	}, | 	}, | ||||||
| 	set: (target, prop, value) => { | 	set: (target, prop, value) => { | ||||||
| @@ -43,9 +48,9 @@ export const evalCode = (code: string) => { | |||||||
|  |  | ||||||
| // deno-lint-ignore no-explicit-any | // deno-lint-ignore no-explicit-any | ||||||
| export const addToContext = (name: string, value: any) => { | export const addToContext = (name: string, value: any) => { | ||||||
| 	G[name] = value; | 	builtins[name] = value; | ||||||
| } | } | ||||||
|  |  | ||||||
| export const getContext = () => { | export const getBuiltins = () => { | ||||||
| 	return G; | 	return builtins; | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user
	 dylan
					dylan