From ac463143ddfa4ba91c65d18dbafdd476cbdbea2c Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 19 Jan 2015 15:20:04 -0700 Subject: [PATCH] Add 1 pixel to scrollWidth to account for cursor if not soft-wrapped --- spec/text-editor-presenter-spec.coffee | 87 ++++++++++++++------------ src/text-editor-presenter.coffee | 5 +- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/spec/text-editor-presenter-spec.coffee b/spec/text-editor-presenter-spec.coffee index 739b21a9f..34d1e1347 100644 --- a/spec/text-editor-presenter-spec.coffee +++ b/spec/text-editor-presenter-spec.coffee @@ -101,9 +101,9 @@ describe "TextEditorPresenter", -> maxLineLength = editor.getMaxScreenLineLength() presenter = new TextEditorPresenter(model: editor, clientHeight: 25, clientWidth: 50, scrollTop: 0, baseCharacterWidth: 10, lineHeight: 10, lineOverdrawMargin: 0) - expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength - expect(presenter.state.lines[line1.id].width).toBe 10 * maxLineLength - expect(presenter.state.lines[line2.id].width).toBe 10 * maxLineLength + expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength + 1 + expect(presenter.state.lines[line1.id].width).toBe 10 * maxLineLength + 1 + expect(presenter.state.lines[line2.id].width).toBe 10 * maxLineLength + 1 presenter = new TextEditorPresenter(model: editor, clientHeight: 25, clientWidth: 10 * maxLineLength + 20, scrollTop: 0, baseCharacterWidth: 10, lineHeight: 10, lineOverdrawMargin: 0) expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength + 20 @@ -249,6 +249,44 @@ describe "TextEditorPresenter", -> expect(presenter.state.lines[line6.id]).toBeUndefined() + describe "when the ::clientWidth changes", -> + it "updates the width of the lines if it changes the ::scrollWidth", -> + line0 = editor.tokenizedLineForScreenRow(0) + line1 = editor.tokenizedLineForScreenRow(1) + line2 = editor.tokenizedLineForScreenRow(2) + + maxLineLength = editor.getMaxScreenLineLength() + + presenter = new TextEditorPresenter(model: editor, clientHeight: 25, clientWidth: 50, scrollTop: 0, scrollWidth: 70, lineHeight: 10, baseCharacterWidth: 10, lineOverdrawMargin: 0) + expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength + 1 + expect(presenter.state.lines[line1.id].width).toBe 10 * maxLineLength + 1 + expect(presenter.state.lines[line2.id].width).toBe 10 * maxLineLength + 1 + + presenter.setClientWidth(10 * maxLineLength + 20) + + expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength + 20 + expect(presenter.state.lines[line1.id].width).toBe 10 * maxLineLength + 20 + expect(presenter.state.lines[line2.id].width).toBe 10 * maxLineLength + 20 + + describe "when the ::baseCharacterWidth changes", -> + it "updates the width of the lines if it changes the ::scrollWidth", -> + line0 = editor.tokenizedLineForScreenRow(0) + line1 = editor.tokenizedLineForScreenRow(1) + line2 = editor.tokenizedLineForScreenRow(2) + + maxLineLength = editor.getMaxScreenLineLength() + + presenter = new TextEditorPresenter(model: editor, clientHeight: 25, clientWidth: 50, scrollTop: 0, scrollWidth: 70, lineHeight: 10, baseCharacterWidth: 10, lineOverdrawMargin: 0) + expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength + 1 + expect(presenter.state.lines[line1.id].width).toBe 10 * maxLineLength + 1 + expect(presenter.state.lines[line2.id].width).toBe 10 * maxLineLength + 1 + + presenter.setBaseCharacterWidth(15) + + expect(presenter.state.lines[line0.id].width).toBe 15 * maxLineLength + 1 + expect(presenter.state.lines[line1.id].width).toBe 15 * maxLineLength + 1 + expect(presenter.state.lines[line2.id].width).toBe 15 * maxLineLength + 1 + describe "when the editor's content changes", -> it "updates the lines state accordingly", -> presenter = new TextEditorPresenter(model: editor, clientHeight: 25, scrollTop: 10, lineHeight: 10, lineOverdrawMargin: 0) @@ -279,40 +317,11 @@ describe "TextEditorPresenter", -> top: 10 * 3 } - describe "when the ::clientWidth changes", -> - it "updates the width of the lines if it changes the ::scrollWidth", -> - line0 = editor.tokenizedLineForScreenRow(0) - line1 = editor.tokenizedLineForScreenRow(1) - line2 = editor.tokenizedLineForScreenRow(2) - - maxLineLength = editor.getMaxScreenLineLength() - + describe "when ::softWrapped changes on the editor", -> + it "only accounts for the cursor width if ::softWrapped is false", -> presenter = new TextEditorPresenter(model: editor, clientHeight: 25, clientWidth: 50, scrollTop: 0, scrollWidth: 70, lineHeight: 10, baseCharacterWidth: 10, lineOverdrawMargin: 0) - expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength - expect(presenter.state.lines[line1.id].width).toBe 10 * maxLineLength - expect(presenter.state.lines[line2.id].width).toBe 10 * maxLineLength - - presenter.setClientWidth(10 * maxLineLength + 20) - - expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength + 20 - expect(presenter.state.lines[line1.id].width).toBe 10 * maxLineLength + 20 - expect(presenter.state.lines[line2.id].width).toBe 10 * maxLineLength + 20 - - describe "when the ::baseCharacterWidth changes", -> - it "updates the width of the lines if it changes the ::scrollWidth", -> - line0 = editor.tokenizedLineForScreenRow(0) - line1 = editor.tokenizedLineForScreenRow(1) - line2 = editor.tokenizedLineForScreenRow(2) - - maxLineLength = editor.getMaxScreenLineLength() - - presenter = new TextEditorPresenter(model: editor, clientHeight: 25, clientWidth: 50, scrollTop: 0, scrollWidth: 70, lineHeight: 10, baseCharacterWidth: 10, lineOverdrawMargin: 0) - expect(presenter.state.lines[line0.id].width).toBe 10 * maxLineLength - expect(presenter.state.lines[line1.id].width).toBe 10 * maxLineLength - expect(presenter.state.lines[line2.id].width).toBe 10 * maxLineLength - - presenter.setBaseCharacterWidth(15) - - expect(presenter.state.lines[line0.id].width).toBe 15 * maxLineLength - expect(presenter.state.lines[line1.id].width).toBe 15 * maxLineLength - expect(presenter.state.lines[line2.id].width).toBe 15 * maxLineLength + expect(presenter.state.lines[editor.tokenizedLineForScreenRow(0).id].width).toBe 10 * editor.getMaxScreenLineLength() + 1 + editor.setSoftWrapped(true) + expect(presenter.state.lines[editor.tokenizedLineForScreenRow(0).id].width).toBe 10 * editor.getMaxScreenLineLength() + editor.setSoftWrapped(false) + expect(presenter.state.lines[editor.tokenizedLineForScreenRow(0).id].width).toBe 10 * editor.getMaxScreenLineLength() + 1 diff --git a/src/text-editor-presenter.coffee b/src/text-editor-presenter.coffee index f3d1fabfb..19341c1e6 100644 --- a/src/text-editor-presenter.coffee +++ b/src/text-editor-presenter.coffee @@ -13,6 +13,7 @@ class TextEditorPresenter subscribeToModel: -> @disposables.add @model.onDidChange(@updateLinesState.bind(this)) + @disposables.add @model.onDidChangeSoftWrapped(@updateLinesState.bind(this)) buildLinesState: -> @state.lines = {} @@ -63,7 +64,9 @@ class TextEditorPresenter Math.min(@model.getScreenLineCount(), endRow) getScrollWidth: -> - Math.max(@model.getMaxScreenLineLength() * @baseCharacterWidth, @clientWidth) + contentWidth = @model.getMaxScreenLineLength() * @baseCharacterWidth + contentWidth += 1 unless @model.isSoftWrapped() # account for cursor width + Math.max(contentWidth, @clientWidth) setScrollTop: (@scrollTop) -> @updateLinesState()