rename caret -> cursor

This commit is contained in:
Dmitry Vasilev
2022-12-03 03:18:54 +08:00
parent e3a742ae77
commit 7e2f2d82b9
6 changed files with 40 additions and 41 deletions

View File

@@ -13,21 +13,21 @@ export const pp_calltree = tree => ({
children: tree.children && tree.children.map(pp_calltree) children: tree.children && tree.children.map(pp_calltree)
}) })
export const current_caret_position = state => export const current_cursor_position = state =>
state.caret_position_by_file[state.current_module] state.cursor_position_by_file[state.current_module]
// When we open file for the first time, caret set to the beginning // When we open file for the first time, cursor set to the beginning
?? 0 ?? 0
export const set_caret_position = (state, caret_position) => ( export const set_cursor_position = (state, cursor_position) => (
{ {
...state, ...state,
caret_position_by_file: { cursor_position_by_file: {
...state.caret_position_by_file, [state.current_module]: caret_position ...state.cursor_position_by_file, [state.current_module]: cursor_position
} }
} }
) )
export const set_location = (state, location) => set_caret_position( export const set_location = (state, location) => set_cursor_position(
{...state, current_module: location.module}, {...state, current_module: location.module},
location.index location.index
) )
@@ -458,7 +458,7 @@ const click = (state, id) => {
const node = find_node(state.calltree, n => n.id == id) const node = find_node(state.calltree, n => n.id == id)
const {state: nextstate, effects} = jump_calltree_node(state, node) const {state: nextstate, effects} = jump_calltree_node(state, node)
if(is_expandable(node)) { if(is_expandable(node)) {
// `effects` are intentionally discarded, correct `set_caret_position` will // `effects` are intentionally discarded, correct `set_cursor_position` will
// be applied in `toggle_expanded` // be applied in `toggle_expanded`
return toggle_expanded(nextstate) return toggle_expanded(nextstate)
} else { } else {

View File

@@ -15,7 +15,7 @@ import {
add_frame, calltree_node_loc, expand_path, add_frame, calltree_node_loc, expand_path,
initial_calltree_node, default_expand_path, toggle_expanded, active_frame, initial_calltree_node, default_expand_path, toggle_expanded, active_frame,
find_call, find_call_node, set_active_calltree_node, find_call, find_call_node, set_active_calltree_node,
set_caret_position, current_caret_position, set_location set_cursor_position, current_cursor_position, set_location
} from './calltree.js' } from './calltree.js'
const collect_logs = call => const collect_logs = call =>
@@ -172,7 +172,7 @@ const external_imports_loaded = (
} }
} }
const node = find_call_node(state, current_caret_position(state)) const node = find_call_node(state, current_cursor_position(state))
let active_calltree_node, next let active_calltree_node, next
@@ -246,7 +246,7 @@ const external_imports_loaded = (
const input = (state, code, index) => { const input = (state, code, index) => {
const files = {...state.files, [state.current_module]: code} const files = {...state.files, [state.current_module]: code}
const next = run_code( const next = run_code(
set_caret_position({...state, files}, index), set_cursor_position({...state, files}, index),
[state.current_module] [state.current_module]
) )
const effect_save = next.current_module == '' const effect_save = next.current_module == ''
@@ -380,7 +380,7 @@ const get_next_selection_state = (selection_state, frame, is_expand, index) => {
n.index <= index && n.index + n.length > index n.index <= index && n.index + n.length > index
) )
?? ??
// caret not inside child but in whitespace // cursor not inside child but in whitespace
selection_state.node selection_state.node
} else { } else {
// no children, cannot collapse // no children, cannot collapse
@@ -523,7 +523,7 @@ const goto_definition = (state, index) => {
loc = {module: state.current_module, index: d.index} loc = {module: state.current_module, index: d.index}
} }
return { return {
state: set_caret_position( state: set_cursor_position(
{...state, current_module: loc.module}, {...state, current_module: loc.module},
loc.index, loc.index,
) )
@@ -710,7 +710,7 @@ const do_move_cursor = (state, index) => {
const move_cursor = (s, index) => { const move_cursor = (s, index) => {
const with_cursor = set_caret_position(s, index) const with_cursor = set_cursor_position(s, index)
if(!s.parse_result.ok){ if(!s.parse_result.ok){
return {state: with_cursor} return {state: with_cursor}
@@ -809,7 +809,7 @@ const get_initial_state = state => {
entrypoint, entrypoint,
current_module, current_module,
html_file, html_file,
caret_position_by_file: {[current_module]: 0}, cursor_position_by_file: {[current_module]: 0},
} }
} }

View File

@@ -91,7 +91,7 @@ export class Editor {
normalize_events(this.ace_editor, { normalize_events(this.ace_editor, {
on_change: () => { on_change: () => {
try { try {
exec('input', this.ace_editor.getValue(), this.get_caret_position()) exec('input', this.ace_editor.getValue(), this.get_cursor_position())
} catch(e) { } catch(e) {
// Do not throw Error to ACE because it breaks typing // Do not throw Error to ACE because it breaks typing
console.error(e) console.error(e)
@@ -106,7 +106,7 @@ export class Editor {
on_change_selection: () => { on_change_selection: () => {
try { try {
if(!this.is_change_selection_supressed) { if(!this.is_change_selection_supressed) {
exec('move_cursor', this.get_caret_position()) exec('move_cursor', this.get_cursor_position())
} }
} catch(e) { } catch(e) {
// Do not throw Error to ACE because it breaks typing // Do not throw Error to ACE because it breaks typing
@@ -331,7 +331,7 @@ export class Editor {
this.ace_editor.commands.addCommand({ this.ace_editor.commands.addCommand({
name: 'step_into', name: 'step_into',
exec: (editor) => { exec: (editor) => {
exec('step_into', this.get_caret_position()) exec('step_into', this.get_cursor_position())
} }
}) })
@@ -354,13 +354,13 @@ export class Editor {
this.ace_editor.commands.addCommand({ this.ace_editor.commands.addCommand({
name: 'expand_selection', name: 'expand_selection',
exec: () => { exec: () => {
exec('eval_selection', this.get_caret_position(), true) exec('eval_selection', this.get_cursor_position(), true)
} }
}) })
this.ace_editor.commands.addCommand({ this.ace_editor.commands.addCommand({
name: 'collapse_selection', name: 'collapse_selection',
exec: () => { exec: () => {
exec('eval_selection', this.get_caret_position(), false) exec('eval_selection', this.get_cursor_position(), false)
} }
}) })
this.ace_editor.commands.bindKey("ctrl-j", 'expand_selection') this.ace_editor.commands.bindKey("ctrl-j", 'expand_selection')
@@ -411,7 +411,7 @@ export class Editor {
} }
get_caret_position(file){ get_cursor_position(file){
const session = file == null const session = file == null
? this.ace_editor.getSession() ? this.ace_editor.getSession()
: this.get_session(file) : this.get_session(file)
@@ -424,13 +424,12 @@ export class Editor {
return session.doc.positionToIndex(session.selection.getCursor()) return session.doc.positionToIndex(session.selection.getCursor())
} }
set_caret_position(index){ set_cursor_position(index){
if(index == null) { if(index == null) {
throw new Error('illegal state') throw new Error('illegal state')
} }
const pos = this.ace_editor.session.doc.indexToPosition(index) const pos = this.ace_editor.session.doc.indexToPosition(index)
console.log('set caret position', index, pos)
this.supress_change_selection(() => { this.supress_change_selection(() => {
const pos = this.ace_editor.session.doc.indexToPosition(index) const pos = this.ace_editor.session.doc.indexToPosition(index)
@@ -446,7 +445,7 @@ export class Editor {
} }
goto_definition(){ goto_definition(){
const index = this.get_caret_position() const index = this.get_cursor_position()
exec('goto_definition', index) exec('goto_definition', index)
} }

View File

@@ -165,7 +165,7 @@ export class UI {
let loc let loc
if((loc = e.target.dataset.location) != null){ if((loc = e.target.dataset.location) != null){
loc = JSON.parse(loc) loc = JSON.parse(loc)
this.editor.set_caret_position(loc.index) this.editor.set_cursor_position(loc.index)
this.editor.focus() this.editor.focus()
} }
} }

6
src/effects.js vendored
View File

@@ -5,7 +5,7 @@ import {
calltree_node_loc, calltree_node_loc,
get_deferred_calls get_deferred_calls
} from './calltree.js' } from './calltree.js'
import {current_caret_position} from './calltree.js' import {current_cursor_position} from './calltree.js'
import {FLAGS} from './feature_flags.js' import {FLAGS} from './feature_flags.js'
import {exec, FILES_ROOT} from './index.js' import {exec, FILES_ROOT} from './index.js'
@@ -166,8 +166,8 @@ export const render_common_side_effects = (prev, next, command, ui) => {
ui.editor.switch_session(next.current_module) ui.editor.switch_session(next.current_module)
} }
if(current_caret_position(next) != ui.editor.get_caret_position()) { if(current_cursor_position(next) != ui.editor.get_cursor_position()) {
ui.editor.set_caret_position(current_caret_position(next)) ui.editor.set_cursor_position(current_cursor_position(next))
} }
if(prev.loading_external_imports_state != next.loading_external_imports_state) { if(prev.loading_external_imports_state != next.loading_external_imports_state) {

View File

@@ -7,7 +7,7 @@ import {
active_frame, active_frame,
pp_calltree, pp_calltree,
get_deferred_calls, get_deferred_calls,
current_caret_position, current_cursor_position,
} from '../src/calltree.js' } from '../src/calltree.js'
import {color_file} from '../src/color.js' import {color_file} from '../src/color.js'
import { import {
@@ -1109,11 +1109,11 @@ export const tests = [
const x_result_1 = COMMANDS.goto_definition(s, entry.indexOf('x*x')) const x_result_1 = COMMANDS.goto_definition(s, entry.indexOf('x*x'))
assert_equal(x_result_1.state.current_module, '') assert_equal(x_result_1.state.current_module, '')
assert_equal(current_caret_position(x_result_1.state), entry.indexOf('x')) assert_equal(current_cursor_position(x_result_1.state), entry.indexOf('x'))
const x_result_2 = COMMANDS.goto_definition(s, entry.indexOf('x')) const x_result_2 = COMMANDS.goto_definition(s, entry.indexOf('x'))
assert_equal(x_result_2.state.current_module, 'a') assert_equal(x_result_2.state.current_module, 'a')
assert_equal(current_caret_position(x_result_2.state), a.indexOf('x = 2')) assert_equal(current_cursor_position(x_result_2.state), a.indexOf('x = 2'))
}), }),
test('assignment', () => { test('assignment', () => {
@@ -1179,7 +1179,7 @@ export const tests = [
const {state, effects} = COMMANDS.step_into(initial, code.indexOf('x()')) const {state, effects} = COMMANDS.step_into(initial, code.indexOf('x()'))
const call_code = state.current_calltree_node.code const call_code = state.current_calltree_node.code
assert_equal(call_code.index, code.indexOf('() =>')) assert_equal(call_code.index, code.indexOf('() =>'))
assert_equal(current_caret_position(state), code.indexOf('() =>')) assert_equal(current_cursor_position(state), code.indexOf('() =>'))
assert_equal(effects.type, 'embed_value_explorer') assert_equal(effects.type, 'embed_value_explorer')
}), }),
@@ -1580,7 +1580,7 @@ const y = x()`
const index = 0 // Where call starts const index = 0 // Where call starts
const call = root_calltree_node(s).children[0] const call = root_calltree_node(s).children[0]
const {state, effects} = COMMANDS.calltree.click(s, call.id) const {state, effects} = COMMANDS.calltree.click(s, call.id)
assert_equal(current_caret_position(state), index) assert_equal(current_cursor_position(state), index)
assert_equal( assert_equal(
effects, effects,
{ {
@@ -1615,7 +1615,7 @@ const y = x()`
const assert_loc = (s, substring, is_assert_node_by_loc) => { const assert_loc = (s, substring, is_assert_node_by_loc) => {
const {state, effects} = COMMANDS.calltree.arrow_right(s) const {state, effects} = COMMANDS.calltree.arrow_right(s)
const index = code.indexOf(substring) const index = code.indexOf(substring)
assert_equal(current_caret_position(state), index) assert_equal(current_cursor_position(state), index)
if(is_assert_node_by_loc) { if(is_assert_node_by_loc) {
assert_equal( assert_equal(
state.calltree_node_by_loc[''][index] == null, state.calltree_node_by_loc[''][index] == null,
@@ -1891,7 +1891,7 @@ const y = x()`
const {state: s3, effects} = COMMANDS.calltree.select_return_value(s2) const {state: s3, effects} = COMMANDS.calltree.select_return_value(s2)
assert_equal(s3.selection_state.result.value, 1) assert_equal(s3.selection_state.result.value, 1)
assert_equal(s3.selection_state.node.index, code.indexOf('x()')) assert_equal(s3.selection_state.node.index, code.indexOf('x()'))
assert_equal(current_caret_position(s3), code.indexOf('x()')) assert_equal(current_cursor_position(s3), code.indexOf('x()'))
assert_equal(effects, {type: 'set_focus'}) assert_equal(effects, {type: 'set_focus'})
}), }),
@@ -1907,7 +1907,7 @@ const y = x()`
const {state: s3, effects} = COMMANDS.calltree.select_return_value(s2) const {state: s3, effects} = COMMANDS.calltree.select_return_value(s2)
assert_equal(s3.selection_state.result.value, 1) assert_equal(s3.selection_state.result.value, 1)
assert_equal(s3.selection_state.node.index, code.indexOf('1')) assert_equal(s3.selection_state.node.index, code.indexOf('1'))
assert_equal(current_caret_position(s3), code.indexOf('1')) assert_equal(current_cursor_position(s3), code.indexOf('1'))
assert_equal(effects, {type: 'set_focus'}) assert_equal(effects, {type: 'set_focus'})
}), }),
@@ -1923,7 +1923,7 @@ const y = x()`
const {state: s3, effects} = COMMANDS.calltree.select_return_value(s2) const {state: s3, effects} = COMMANDS.calltree.select_return_value(s2)
assert_equal(s3.selection_state.result.value, 1) assert_equal(s3.selection_state.result.value, 1)
assert_equal(s3.selection_state.node.index, code.indexOf('1')) assert_equal(s3.selection_state.node.index, code.indexOf('1'))
assert_equal(current_caret_position(s3), code.indexOf('1')) assert_equal(current_cursor_position(s3), code.indexOf('1'))
assert_equal(effects, {type: 'set_focus'}) assert_equal(effects, {type: 'set_focus'})
}), }),
@@ -1938,7 +1938,7 @@ const y = x()`
const s2 = COMMANDS.calltree.arrow_right(s2_0).state const s2 = COMMANDS.calltree.arrow_right(s2_0).state
const {state: s3, effects} = COMMANDS.calltree.select_return_value(s2) const {state: s3, effects} = COMMANDS.calltree.select_return_value(s2)
assert_equal(s3.selection_state, null) assert_equal(s3.selection_state, null)
assert_equal(current_caret_position(s3), code.indexOf('{')) assert_equal(current_cursor_position(s3), code.indexOf('{'))
assert_equal(effects, {type: 'set_focus'}) assert_equal(effects, {type: 'set_focus'})
}), }),
@@ -1963,7 +1963,7 @@ const y = x()`
const s2 = COMMANDS.calltree.arrow_right(s1).state const s2 = COMMANDS.calltree.arrow_right(s1).state
const s3 = COMMANDS.calltree.select_arguments(s2) const s3 = COMMANDS.calltree.select_arguments(s2)
assert_equal(s3.state.selection_state.result, {ok: true, value: [1]}) assert_equal(s3.state.selection_state.result, {ok: true, value: [1]})
assert_equal(current_caret_position(s3.state), code.indexOf('(1)')) assert_equal(current_cursor_position(s3.state), code.indexOf('(1)'))
assert_equal(s3.effects, {type: 'set_focus'}) assert_equal(s3.effects, {type: 'set_focus'})
}), }),
@@ -1979,7 +1979,7 @@ const y = x()`
const s2 = COMMANDS.calltree.arrow_right(s2_0).state const s2 = COMMANDS.calltree.arrow_right(s2_0).state
const s3 = COMMANDS.calltree.select_arguments(s2) const s3 = COMMANDS.calltree.select_arguments(s2)
assert_equal(s3.state.selection_state.result, {ok: true, value: {a: 1}}) assert_equal(s3.state.selection_state.result, {ok: true, value: {a: 1}})
assert_equal(current_caret_position(s3.state), code.indexOf('(a)')) assert_equal(current_cursor_position(s3.state), code.indexOf('(a)'))
assert_equal(s3.effects, {type: 'set_focus'}) assert_equal(s3.effects, {type: 'set_focus'})
}), }),
@@ -2346,7 +2346,7 @@ const y = x()`
const {state, effects} = COMMANDS.calltree.navigate_logs_position(i, 0) const {state, effects} = COMMANDS.calltree.navigate_logs_position(i, 0)
assert_equal(state.logs.log_position, 0) assert_equal(state.logs.log_position, 0)
assert_equal(state.selection_state.result.value, [10]) assert_equal(state.selection_state.result.value, [10])
assert_equal(current_caret_position(state), code.indexOf('(x)')) assert_equal(current_cursor_position(state), code.indexOf('(x)'))
}), }),
test('deferred calls', () => { test('deferred calls', () => {