Skip to content

Commit

Permalink
Merge pull request #1035 from pulsar-edit/revert-810
Browse files Browse the repository at this point in the history
Revert "Merge pull request #810 from pulsar-edit/fix-on-change-cursor-pos"
  • Loading branch information
savetheclocktower authored Jun 27, 2024
2 parents 6324230 + 1112c72 commit 6a2b5bc
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 266 deletions.
111 changes: 17 additions & 94 deletions spec/text-editor-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,104 +353,27 @@ describe('TextEditor', () => {
expect(editor.getCursorBufferPosition()).toEqual([1, 1]);
});

describe('listening to cursor movements', () => {
it('emits an event with the old position, new position, and the cursor that moved', () => {
const cursorCallback = jasmine.createSpy('cursor-changed-position');
const editorCallback = jasmine.createSpy(
'editor-changed-cursor-position'
);
it('emits an event with the old position, new position, and the cursor that moved', () => {
const cursorCallback = jasmine.createSpy('cursor-changed-position');
const editorCallback = jasmine.createSpy(
'editor-changed-cursor-position'
);

editor.getLastCursor().onDidChangePosition(cursorCallback);
editor.onDidChangeCursorPosition(editorCallback);
editor.getLastCursor().onDidChangePosition(cursorCallback);
editor.onDidChangeCursorPosition(editorCallback);

editor.setCursorBufferPosition([2, 4]);
editor.setCursorBufferPosition([2, 4]);

expect(editorCallback).toHaveBeenCalled();
expect(cursorCallback).toHaveBeenCalled();
const eventObject = editorCallback.mostRecentCall.args[0];
expect(cursorCallback.mostRecentCall.args[0]).toEqual(eventObject);

expect(eventObject.oldBufferPosition).toEqual([0, 0]);
expect(eventObject.oldScreenPosition).toEqual([0, 0]);
expect(eventObject.newBufferPosition).toEqual([2, 4]);
expect(eventObject.newScreenPosition).toEqual([2, 4]);
expect(eventObject.cursor).toBe(editor.getLastCursor());
});

it('emits the event with textChanged: true if text was edited', () => {
let callbacks = []
let callback = evt => { callbacks.push(evt.textChanged) }
editor.getLastCursor().onDidChangePosition(callback);
editor.onDidChangeCursorPosition(callback);

editor.insertText('bar')
expect(callbacks).toEqual([true, true])

const cmds = [
'backspace',
'deleteToBeginningOfWord',
'deleteToBeginningOfSubword',
'deleteToPreviousWordBoundary',
'deleteToBeginningOfLine'
]
cmds.forEach(command => {
editor.setText("HelloWorld!")
editor.setCursorBufferPosition([0, 5])
callbacks = []
editor[command].bind(editor)();
expect(callbacks).toEqual([true, true], `on command ${command}`)
})
});

it('emits the event with textChanged: true if whole lines were changed', () => {
let callbacks = [];
let callback = evt => { callbacks.push(evt.textChanged) };
editor.getLastCursor().onDidChangePosition(callback);
editor.onDidChangeCursorPosition(callback);

editor.setText("HelloWorld!\nGoodbye, world");
editor.setCursorBufferPosition([0, 5]);
// TODO: Ideally, we want this event to not be called. Unfortunately,
// the world doesn't work like that - there's no way to delete a line
// without changing the current cursor position on TextBuffer.
callbacks = [];
editor.deleteLine();
// One for the change, and another to reposition the cursor
expect(callbacks).toEqual([true, true, false, false], "on command deleteLine")

editor.setText("HelloWorld!\nGoodbye, world");
editor.setCursorBufferPosition([0, 5]);
callbacks = [];
editor.joinLines();
// TODO: Again, not ideal. But still...
// One for moving to the line that will be deleted, one for the actual change
// and one to move to the "join position" between the lines
expect(callbacks).toEqual([false, false, true, true, false, false], "on command joinLines")
});
expect(editorCallback).toHaveBeenCalled();
expect(cursorCallback).toHaveBeenCalled();
const eventObject = editorCallback.mostRecentCall.args[0];
expect(cursorCallback.mostRecentCall.args[0]).toEqual(eventObject);

it("doesn't emit the event if you deleted something forward", () => {
let callbacks = []
let callback = evt => {
callbacks.push(evt.textChanged)
}
editor.getLastCursor().onDidChangePosition(callback);
editor.onDidChangeCursorPosition(callback);

const cmds = [
'delete',
'deleteToEndOfSubword',
'deleteToEndOfWord',
'deleteToEndOfLine',
'deleteToNextWordBoundary'
]
cmds.forEach(command => {
editor.setText("HelloWorld!")
editor.setCursorBufferPosition([0, 5])
callbacks = []
editor[command].bind(editor)();
expect(callbacks).toEqual([], `on command ${command}`)
})
});
expect(eventObject.oldBufferPosition).toEqual([0, 0]);
expect(eventObject.oldScreenPosition).toEqual([0, 0]);
expect(eventObject.newBufferPosition).toEqual([2, 4]);
expect(eventObject.newScreenPosition).toEqual([2, 4]);
expect(eventObject.cursor).toBe(editor.getLastCursor());
});
});

Expand Down
48 changes: 4 additions & 44 deletions src/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,25 +300,6 @@ module.exports = class Cursor extends Model {
const range = this.marker.getScreenRange();
if (moveToEndOfSelection && !range.isEmpty()) {
this.setScreenPosition(range.start);
} else {
const point = this.getPreviousColumnScreenPosition(
columnCount, { moveToEndOfSelection }
)
this.setScreenPosition(point, { clipDirection: 'backward' });
}
}

// Public: Retrieves the screen position of where the previous column starts.
// * `columnCount` (optional) {Number} number of columns to move (default: 1)
// * `options` (optional) {Object} with the following keys:
// * `moveToEndOfSelection` if true, move to the right of the selection if a
// selection exists.
//
// Returns a {Point}.
getPreviousColumnScreenPosition(columnCount = 1, { moveToEndOfSelection } = {}) {
const range = this.marker.getScreenRange();
if (moveToEndOfSelection && !range.isEmpty()) {
return range.start;
} else {
let { row, column } = this.getScreenPosition();

Expand All @@ -329,7 +310,7 @@ module.exports = class Cursor extends Model {
}

column = column - columnCount;
return new Point( row, column )
this.setScreenPosition({ row, column }, { clipDirection: 'backward' });
}
}

Expand All @@ -343,25 +324,6 @@ module.exports = class Cursor extends Model {
const range = this.marker.getScreenRange();
if (moveToEndOfSelection && !range.isEmpty()) {
this.setScreenPosition(range.end);
} else {
const point = this.getNextColumnScreenPosition(
columnCount, { moveToEndOfSelection }
)
this.setScreenPosition(point, { clipDirection: 'forward' });
}
}

// Public: Retrieves the screen position of where the next column starts.
// * `columnCount` (optional) {Number} number of columns to move (default: 1)
// * `options` (optional) {Object} with the following keys:
// * `moveToEndOfSelection` if true, move to the right of the selection if a
// selection exists.
//
// Returns a {Point}.
getNextColumnScreenPosition(columnCount = 1, { moveToEndOfSelection } = {}) {
const range = this.marker.getScreenRange();
if (moveToEndOfSelection && !range.isEmpty()) {
return range.end;
} else {
let { row, column } = this.getScreenPosition();
const maxLines = this.editor.getScreenLineCount();
Expand All @@ -378,7 +340,7 @@ module.exports = class Cursor extends Model {
}

column = column + columnCount;
return new Point(row, column);
this.setScreenPosition({ row, column }, { clipDirection: 'forward' });
}
}

Expand Down Expand Up @@ -606,7 +568,7 @@ module.exports = class Cursor extends Model {
// * `allowPrevious` A {Boolean} indicating whether the beginning of the
// previous word can be returned.
//
// Returns a {Point}.
// Returns a {Range}.
getBeginningOfCurrentWordBufferPosition(options = {}) {
const allowPrevious = options.allowPrevious !== false;
const position = this.getBufferPosition();
Expand Down Expand Up @@ -667,7 +629,7 @@ module.exports = class Cursor extends Model {
// * `wordRegex` A {RegExp} indicating what constitutes a "word"
// (default: {::wordRegExp}).
//
// Returns a {Point}
// Returns a {Range}
getBeginningOfNextWordBufferPosition(options = {}) {
const currentBufferPosition = this.getBufferPosition();
const start = this.isInsideWord(options)
Expand Down Expand Up @@ -712,8 +674,6 @@ module.exports = class Cursor extends Model {
// * `options` (optional) {Object}
// * `includeNewline` A {Boolean} which controls whether the Range should
// include the newline.
//
// Returns a {Range}.
getCurrentLineBufferRange(options) {
return this.editor.bufferRangeForBufferRow(this.getBufferRow(), options);
}
Expand Down
Loading

0 comments on commit 6a2b5bc

Please sign in to comment.