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)
})
export const current_caret_position = state =>
state.caret_position_by_file[state.current_module]
// When we open file for the first time, caret set to the beginning
export const current_cursor_position = state =>
state.cursor_position_by_file[state.current_module]
// When we open file for the first time, cursor set to the beginning
?? 0
export const set_caret_position = (state, caret_position) => (
export const set_cursor_position = (state, cursor_position) => (
{
...state,
caret_position_by_file: {
...state.caret_position_by_file, [state.current_module]: caret_position
cursor_position_by_file: {
...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},
location.index
)
@@ -458,7 +458,7 @@ const click = (state, id) => {
const node = find_node(state.calltree, n => n.id == id)
const {state: nextstate, effects} = jump_calltree_node(state, 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`
return toggle_expanded(nextstate)
} else {

View File

@@ -15,7 +15,7 @@ import {
add_frame, calltree_node_loc, expand_path,
initial_calltree_node, default_expand_path, toggle_expanded, active_frame,
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'
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
@@ -246,7 +246,7 @@ const external_imports_loaded = (
const input = (state, code, index) => {
const files = {...state.files, [state.current_module]: code}
const next = run_code(
set_caret_position({...state, files}, index),
set_cursor_position({...state, files}, index),
[state.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
)
??
// caret not inside child but in whitespace
// cursor not inside child but in whitespace
selection_state.node
} else {
// no children, cannot collapse
@@ -523,7 +523,7 @@ const goto_definition = (state, index) => {
loc = {module: state.current_module, index: d.index}
}
return {
state: set_caret_position(
state: set_cursor_position(
{...state, current_module: loc.module},
loc.index,
)
@@ -710,7 +710,7 @@ const do_move_cursor = (state, 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){
return {state: with_cursor}
@@ -809,7 +809,7 @@ const get_initial_state = state => {
entrypoint,
current_module,
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, {
on_change: () => {
try {
exec('input', this.ace_editor.getValue(), this.get_caret_position())
exec('input', this.ace_editor.getValue(), this.get_cursor_position())
} catch(e) {
// Do not throw Error to ACE because it breaks typing
console.error(e)
@@ -106,7 +106,7 @@ export class Editor {
on_change_selection: () => {
try {
if(!this.is_change_selection_supressed) {
exec('move_cursor', this.get_caret_position())
exec('move_cursor', this.get_cursor_position())
}
} catch(e) {
// Do not throw Error to ACE because it breaks typing
@@ -331,7 +331,7 @@ export class Editor {
this.ace_editor.commands.addCommand({
name: 'step_into',
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({
name: 'expand_selection',
exec: () => {
exec('eval_selection', this.get_caret_position(), true)
exec('eval_selection', this.get_cursor_position(), true)
}
})
this.ace_editor.commands.addCommand({
name: 'collapse_selection',
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')
@@ -411,7 +411,7 @@ export class Editor {
}
get_caret_position(file){
get_cursor_position(file){
const session = file == null
? this.ace_editor.getSession()
: this.get_session(file)
@@ -424,13 +424,12 @@ export class Editor {
return session.doc.positionToIndex(session.selection.getCursor())
}
set_caret_position(index){
set_cursor_position(index){
if(index == null) {
throw new Error('illegal state')
}
const pos = this.ace_editor.session.doc.indexToPosition(index)
console.log('set caret position', index, pos)
this.supress_change_selection(() => {
const pos = this.ace_editor.session.doc.indexToPosition(index)
@@ -446,7 +445,7 @@ export class Editor {
}
goto_definition(){
const index = this.get_caret_position()
const index = this.get_cursor_position()
exec('goto_definition', index)
}

View File

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

6
src/effects.js vendored
View File

@@ -5,7 +5,7 @@ import {
calltree_node_loc,
get_deferred_calls
} from './calltree.js'
import {current_caret_position} from './calltree.js'
import {current_cursor_position} from './calltree.js'
import {FLAGS} from './feature_flags.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)
}
if(current_caret_position(next) != ui.editor.get_caret_position()) {
ui.editor.set_caret_position(current_caret_position(next))
if(current_cursor_position(next) != ui.editor.get_cursor_position()) {
ui.editor.set_cursor_position(current_cursor_position(next))
}
if(prev.loading_external_imports_state != next.loading_external_imports_state) {