Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
selection-count-view optional chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jul 17, 2020
1 parent 2d08339 commit be21b5d
Showing 1 changed file with 15 additions and 39 deletions.
54 changes: 15 additions & 39 deletions lib/selection-count-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,26 @@ export default class SelectionCountView {

destroy() {
this.activeItemSubscription.dispose();
if (this.selectionSubscription) {
this.selectionSubscription.dispose();
}
if (this.configSubscription) {
this.configSubscription.dispose();
}
this.selectionSubscription?.dispose();
this.configSubscription?.dispose();
this.tooltipDisposable.dispose();
}

subscribeToConfig() {
if (this.configSubscription) {
this.configSubscription.dispose();
}
this.configSubscription = atom.config.observe(
'status-bar.selectionCountFormat',
value => {
this.formatString = value ? value : '(%L, %C)';
this.scheduleUpdateCount();
}
this.configSubscription?.dispose();
this.configSubscription = atom.config.observe('status-bar.selectionCountFormat',
value => {
this.formatString = value ? value : '(%L, %C)';
this.scheduleUpdateCount();
}
);
}

subscribeToActiveTextEditor() {
if (this.selectionSubscription) {
this.selectionSubscription.dispose();
}
this.selectionSubscription?.dispose();
const activeEditor = this.getActiveTextEditor();
const selectionsMarkerLayer = activeEditor
? activeEditor.selectionsMarkerLayer
: undefined;
this.selectionSubscription = selectionsMarkerLayer
? selectionsMarkerLayer.onDidUpdate(this.scheduleUpdateCount.bind(this))
: undefined;
const selectionsMarkerLayer = activeEditor?.selectionsMarkerLayer;
this.selectionSubscription = selectionsMarkerLayer?.onDidUpdate(this.scheduleUpdateCount.bind(this));
this.scheduleUpdateCount();
}

Expand All @@ -80,22 +67,11 @@ export default class SelectionCountView {
}

updateCount() {
// optional chaining rewritten:
let count, range;
const editor = atom.workspace.getActiveTextEditor();
if (editor) {
count = editor.getSelectedText().length;
range = editor.getSelectedBufferRange();
}

let lineCount, rangeEndColumn;
if (range) {
lineCount = range.getRowCount();
rangeEndColumn = range.end.column;
}
if (rangeEndColumn === 0) {
lineCount -= 1;
}
const count = editor?.getSelectedText().length;
const range = editor?.getSelectedBufferRange();
let lineCount = range?.getRowCount();
if (range?.end.column === 0) { lineCount -= 1; }
if (count > 0) {
this.element.textContent = this.formatString
.replace('%L', lineCount)
Expand Down

0 comments on commit be21b5d

Please sign in to comment.