From b2c5049b72dea8233e483dd39e3aaffe1a04fb5e Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Sun, 26 Mar 2023 03:57:59 -0700 Subject: [PATCH 1/2] Add machine decaf files for source --- packages/wrap-guide/lib/main.js | 39 ++++ packages/wrap-guide/lib/wrap-guide-element.js | 196 ++++++++++++++++++ 2 files changed, 235 insertions(+) create mode 100644 packages/wrap-guide/lib/main.js create mode 100644 packages/wrap-guide/lib/wrap-guide-element.js diff --git a/packages/wrap-guide/lib/main.js b/packages/wrap-guide/lib/main.js new file mode 100644 index 000000000..39af7c391 --- /dev/null +++ b/packages/wrap-guide/lib/main.js @@ -0,0 +1,39 @@ +/* + * decaffeinate suggestions: + * DS102: Remove unnecessary code created because of implicit returns + * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md + */ +const {CompositeDisposable} = require('atom'); +const WrapGuideElement = require('./wrap-guide-element'); + +module.exports = { + activate() { + this.subscriptions = new CompositeDisposable(); + this.wrapGuides = new Map(); + + return this.subscriptions.add(atom.workspace.observeTextEditors(editor => { + if (this.wrapGuides.has(editor)) { return; } + + const editorElement = atom.views.getView(editor); + const wrapGuideElement = new WrapGuideElement(editor, editorElement); + + this.wrapGuides.set(editor, wrapGuideElement); + return this.subscriptions.add(editor.onDidDestroy(() => { + this.wrapGuides.get(editor).destroy(); + return this.wrapGuides.delete(editor); + }) + ); + }) + ); + }, + + deactivate() { + this.subscriptions.dispose(); + this.wrapGuides.forEach((wrapGuide, editor) => wrapGuide.destroy()); + return this.wrapGuides.clear(); + }, + + uniqueAscending(list) { + return (list.filter((item, index) => list.indexOf(item) === index)).sort((a, b) => a - b); + } +}; diff --git a/packages/wrap-guide/lib/wrap-guide-element.js b/packages/wrap-guide/lib/wrap-guide-element.js new file mode 100644 index 000000000..53b7fadf2 --- /dev/null +++ b/packages/wrap-guide/lib/wrap-guide-element.js @@ -0,0 +1,196 @@ +/* + * decaffeinate suggestions: + * DS101: Remove unnecessary use of Array.from + * DS102: Remove unnecessary code created because of implicit returns + * DS104: Avoid inline assignments + * DS205: Consider reworking code to avoid use of IIFEs + * DS207: Consider shorter variations of null checks + * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md + */ +let WrapGuideElement; +const {CompositeDisposable} = require('atom'); + +module.exports = +(WrapGuideElement = class WrapGuideElement { + constructor(editor, editorElement) { + this.editor = editor; + this.editorElement = editorElement; + this.subscriptions = new CompositeDisposable(); + this.configSubscriptions = new CompositeDisposable(); + this.element = document.createElement('div'); + this.element.setAttribute('is', 'wrap-guide'); + this.element.classList.add('wrap-guide-container'); + this.attachToLines(); + this.handleEvents(); + this.updateGuide(); + + this.element.updateGuide = this.updateGuide.bind(this); + this.element.getDefaultColumn = this.getDefaultColumn.bind(this); + } + + attachToLines() { + const scrollView = this.editorElement.querySelector('.scroll-view'); + return (scrollView != null ? scrollView.appendChild(this.element) : undefined); + } + + handleEvents() { + const updateGuideCallback = () => this.updateGuide(); + + this.handleConfigEvents(); + + this.subscriptions.add(atom.config.onDidChange('editor.fontSize', () => { + // Wait for editor to finish updating before updating wrap guide + // TODO: Use async/await once this file is converted to JS + return this.editorElement.getComponent().getNextUpdatePromise().then(() => updateGuideCallback()); + }) + ); + + this.subscriptions.add(this.editorElement.onDidChangeScrollLeft(updateGuideCallback)); + this.subscriptions.add(this.editor.onDidChangePath(updateGuideCallback)); + this.subscriptions.add(this.editor.onDidChangeGrammar(() => { + this.configSubscriptions.dispose(); + this.handleConfigEvents(); + return updateGuideCallback(); + }) + ); + + this.subscriptions.add(this.editor.onDidDestroy(() => { + this.subscriptions.dispose(); + return this.configSubscriptions.dispose(); + }) + ); + + return this.subscriptions.add(this.editorElement.onDidAttach(() => { + this.attachToLines(); + return updateGuideCallback(); + }) + ); + } + + handleConfigEvents() { + const {uniqueAscending} = require('./main'); + + const updatePreferredLineLengthCallback = args => { + // ensure that the right-most wrap guide is the preferredLineLength + let columns = atom.config.get('wrap-guide.columns', {scope: this.editor.getRootScopeDescriptor()}); + if (columns.length > 0) { + columns[columns.length - 1] = args.newValue; + columns = uniqueAscending(Array.from(columns).filter((i) => i <= args.newValue)); + atom.config.set('wrap-guide.columns', columns, + {scopeSelector: `.${this.editor.getGrammar().scopeName}`}); + } + return this.updateGuide(); + }; + this.configSubscriptions.add(atom.config.onDidChange( + 'editor.preferredLineLength', + {scope: this.editor.getRootScopeDescriptor()}, + updatePreferredLineLengthCallback + ) + ); + + const updateGuideCallback = () => this.updateGuide(); + this.configSubscriptions.add(atom.config.onDidChange( + 'wrap-guide.enabled', + {scope: this.editor.getRootScopeDescriptor()}, + updateGuideCallback + ) + ); + + const updateGuidesCallback = args => { + // ensure that multiple guides stay sorted in ascending order + const columns = uniqueAscending(args.newValue); + if (columns != null ? columns.length : undefined) { + atom.config.set('wrap-guide.columns', columns); + atom.config.set('editor.preferredLineLength', columns[columns.length - 1], + {scopeSelector: `.${this.editor.getGrammar().scopeName}`}); + return this.updateGuide(); + } + }; + return this.configSubscriptions.add(atom.config.onDidChange( + 'wrap-guide.columns', + {scope: this.editor.getRootScopeDescriptor()}, + updateGuidesCallback + ) + ); + } + + getDefaultColumn() { + return atom.config.get('editor.preferredLineLength', {scope: this.editor.getRootScopeDescriptor()}); + } + + getGuidesColumns(path, scopeName) { + let left; + const columns = (left = atom.config.get('wrap-guide.columns', {scope: this.editor.getRootScopeDescriptor()})) != null ? left : []; + if (columns.length > 0) { return columns; } + return [this.getDefaultColumn()]; + } + + isEnabled() { + let left; + return (left = atom.config.get('wrap-guide.enabled', {scope: this.editor.getRootScopeDescriptor()})) != null ? left : true; + } + + hide() { + return this.element.style.display = 'none'; + } + + show() { + return this.element.style.display = 'block'; + } + + updateGuide() { + if (this.isEnabled()) { + return this.updateGuides(); + } else { + return this.hide(); + } + } + + updateGuides() { + this.removeGuides(); + this.appendGuides(); + if (this.element.children.length) { + return this.show(); + } else { + return this.hide(); + } + } + + destroy() { + this.element.remove(); + this.subscriptions.dispose(); + return this.configSubscriptions.dispose(); + } + + removeGuides() { + return (() => { + const result = []; + while (this.element.firstChild) { + result.push(this.element.removeChild(this.element.firstChild)); + } + return result; + })(); + } + + appendGuides() { + const columns = this.getGuidesColumns(this.editor.getPath(), this.editor.getGrammar().scopeName); + return (() => { + const result = []; + for (let column of Array.from(columns)) { + if (!(column < 0)) { result.push(this.appendGuide(column)); } else { + result.push(undefined); + } + } + return result; + })(); + } + + appendGuide(column) { + let columnWidth = this.editorElement.getDefaultCharacterWidth() * column; + columnWidth -= this.editorElement.getScrollLeft(); + const guide = document.createElement('div'); + guide.classList.add('wrap-guide'); + guide.style.left = `${Math.round(columnWidth)}px`; + return this.element.appendChild(guide); + } +}); From c91150762210842e6ab14d9d8080bbc4f3d7957c Mon Sep 17 00:00:00 2001 From: confused-Techie Date: Sun, 26 Mar 2023 04:05:01 -0700 Subject: [PATCH 2/2] Manual decaf of `wrap-guide` --- packages/wrap-guide/lib/main.coffee | 26 ---- packages/wrap-guide/lib/main.js | 11 +- .../wrap-guide/lib/wrap-guide-element.coffee | 137 ------------------ packages/wrap-guide/lib/wrap-guide-element.js | 31 ++-- 4 files changed, 14 insertions(+), 191 deletions(-) delete mode 100644 packages/wrap-guide/lib/main.coffee delete mode 100644 packages/wrap-guide/lib/wrap-guide-element.coffee diff --git a/packages/wrap-guide/lib/main.coffee b/packages/wrap-guide/lib/main.coffee deleted file mode 100644 index e2beff8d7..000000000 --- a/packages/wrap-guide/lib/main.coffee +++ /dev/null @@ -1,26 +0,0 @@ -{CompositeDisposable} = require 'atom' -WrapGuideElement = require './wrap-guide-element' - -module.exports = - activate: -> - @subscriptions = new CompositeDisposable() - @wrapGuides = new Map() - - @subscriptions.add atom.workspace.observeTextEditors (editor) => - return if @wrapGuides.has(editor) - - editorElement = atom.views.getView(editor) - wrapGuideElement = new WrapGuideElement(editor, editorElement) - - @wrapGuides.set(editor, wrapGuideElement) - @subscriptions.add editor.onDidDestroy => - @wrapGuides.get(editor).destroy() - @wrapGuides.delete(editor) - - deactivate: -> - @subscriptions.dispose() - @wrapGuides.forEach (wrapGuide, editor) -> wrapGuide.destroy() - @wrapGuides.clear() - - uniqueAscending: (list) -> - (list.filter((item, index) -> list.indexOf(item) is index)).sort((a, b) -> a - b) diff --git a/packages/wrap-guide/lib/main.js b/packages/wrap-guide/lib/main.js index 39af7c391..e9380d4eb 100644 --- a/packages/wrap-guide/lib/main.js +++ b/packages/wrap-guide/lib/main.js @@ -1,8 +1,3 @@ -/* - * decaffeinate suggestions: - * DS102: Remove unnecessary code created because of implicit returns - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md - */ const {CompositeDisposable} = require('atom'); const WrapGuideElement = require('./wrap-guide-element'); @@ -11,16 +6,16 @@ module.exports = { this.subscriptions = new CompositeDisposable(); this.wrapGuides = new Map(); - return this.subscriptions.add(atom.workspace.observeTextEditors(editor => { + this.subscriptions.add(atom.workspace.observeTextEditors(editor => { if (this.wrapGuides.has(editor)) { return; } const editorElement = atom.views.getView(editor); const wrapGuideElement = new WrapGuideElement(editor, editorElement); this.wrapGuides.set(editor, wrapGuideElement); - return this.subscriptions.add(editor.onDidDestroy(() => { + this.subscriptions.add(editor.onDidDestroy(() => { this.wrapGuides.get(editor).destroy(); - return this.wrapGuides.delete(editor); + this.wrapGuides.delete(editor); }) ); }) diff --git a/packages/wrap-guide/lib/wrap-guide-element.coffee b/packages/wrap-guide/lib/wrap-guide-element.coffee deleted file mode 100644 index 46f86362b..000000000 --- a/packages/wrap-guide/lib/wrap-guide-element.coffee +++ /dev/null @@ -1,137 +0,0 @@ -{CompositeDisposable} = require 'atom' - -module.exports = -class WrapGuideElement - constructor: (@editor, @editorElement) -> - @subscriptions = new CompositeDisposable() - @configSubscriptions = new CompositeDisposable() - @element = document.createElement('div') - @element.setAttribute('is', 'wrap-guide') - @element.classList.add('wrap-guide-container') - @attachToLines() - @handleEvents() - @updateGuide() - - @element.updateGuide = @updateGuide.bind(this) - @element.getDefaultColumn = @getDefaultColumn.bind(this) - - attachToLines: -> - scrollView = @editorElement.querySelector('.scroll-view') - scrollView?.appendChild(@element) - - handleEvents: -> - updateGuideCallback = => @updateGuide() - - @handleConfigEvents() - - @subscriptions.add atom.config.onDidChange 'editor.fontSize', => - # Wait for editor to finish updating before updating wrap guide - # TODO: Use async/await once this file is converted to JS - @editorElement.getComponent().getNextUpdatePromise().then -> updateGuideCallback() - - @subscriptions.add @editorElement.onDidChangeScrollLeft(updateGuideCallback) - @subscriptions.add @editor.onDidChangePath(updateGuideCallback) - @subscriptions.add @editor.onDidChangeGrammar => - @configSubscriptions.dispose() - @handleConfigEvents() - updateGuideCallback() - - @subscriptions.add @editor.onDidDestroy => - @subscriptions.dispose() - @configSubscriptions.dispose() - - @subscriptions.add @editorElement.onDidAttach => - @attachToLines() - updateGuideCallback() - - handleConfigEvents: -> - {uniqueAscending} = require './main' - - updatePreferredLineLengthCallback = (args) => - # ensure that the right-most wrap guide is the preferredLineLength - columns = atom.config.get('wrap-guide.columns', scope: @editor.getRootScopeDescriptor()) - if columns.length > 0 - columns[columns.length - 1] = args.newValue - columns = uniqueAscending(i for i in columns when i <= args.newValue) - atom.config.set 'wrap-guide.columns', columns, - scopeSelector: ".#{@editor.getGrammar().scopeName}" - @updateGuide() - @configSubscriptions.add atom.config.onDidChange( - 'editor.preferredLineLength', - scope: @editor.getRootScopeDescriptor(), - updatePreferredLineLengthCallback - ) - - updateGuideCallback = => @updateGuide() - @configSubscriptions.add atom.config.onDidChange( - 'wrap-guide.enabled', - scope: @editor.getRootScopeDescriptor(), - updateGuideCallback - ) - - updateGuidesCallback = (args) => - # ensure that multiple guides stay sorted in ascending order - columns = uniqueAscending(args.newValue) - if columns?.length - atom.config.set('wrap-guide.columns', columns) - atom.config.set 'editor.preferredLineLength', columns[columns.length - 1], - scopeSelector: ".#{@editor.getGrammar().scopeName}" - @updateGuide() - @configSubscriptions.add atom.config.onDidChange( - 'wrap-guide.columns', - scope: @editor.getRootScopeDescriptor(), - updateGuidesCallback - ) - - getDefaultColumn: -> - atom.config.get('editor.preferredLineLength', scope: @editor.getRootScopeDescriptor()) - - getGuidesColumns: (path, scopeName) -> - columns = atom.config.get('wrap-guide.columns', scope: @editor.getRootScopeDescriptor()) ? [] - return columns if columns.length > 0 - return [@getDefaultColumn()] - - isEnabled: -> - atom.config.get('wrap-guide.enabled', scope: @editor.getRootScopeDescriptor()) ? true - - hide: -> - @element.style.display = 'none' - - show: -> - @element.style.display = 'block' - - updateGuide: -> - if @isEnabled() - @updateGuides() - else - @hide() - - updateGuides: -> - @removeGuides() - @appendGuides() - if @element.children.length - @show() - else - @hide() - - destroy: -> - @element.remove() - @subscriptions.dispose() - @configSubscriptions.dispose() - - removeGuides: -> - while @element.firstChild - @element.removeChild(@element.firstChild) - - appendGuides: -> - columns = @getGuidesColumns(@editor.getPath(), @editor.getGrammar().scopeName) - for column in columns - @appendGuide(column) unless column < 0 - - appendGuide: (column) -> - columnWidth = @editorElement.getDefaultCharacterWidth() * column - columnWidth -= @editorElement.getScrollLeft() - guide = document.createElement('div') - guide.classList.add('wrap-guide') - guide.style.left = "#{Math.round(columnWidth)}px" - @element.appendChild(guide) diff --git a/packages/wrap-guide/lib/wrap-guide-element.js b/packages/wrap-guide/lib/wrap-guide-element.js index 53b7fadf2..afb4f2143 100644 --- a/packages/wrap-guide/lib/wrap-guide-element.js +++ b/packages/wrap-guide/lib/wrap-guide-element.js @@ -1,17 +1,6 @@ -/* - * decaffeinate suggestions: - * DS101: Remove unnecessary use of Array.from - * DS102: Remove unnecessary code created because of implicit returns - * DS104: Avoid inline assignments - * DS205: Consider reworking code to avoid use of IIFEs - * DS207: Consider shorter variations of null checks - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md - */ -let WrapGuideElement; const {CompositeDisposable} = require('atom'); -module.exports = -(WrapGuideElement = class WrapGuideElement { +module.exports = class WrapGuideElement { constructor(editor, editorElement) { this.editor = editor; this.editorElement = editorElement; @@ -41,7 +30,7 @@ module.exports = this.subscriptions.add(atom.config.onDidChange('editor.fontSize', () => { // Wait for editor to finish updating before updating wrap guide // TODO: Use async/await once this file is converted to JS - return this.editorElement.getComponent().getNextUpdatePromise().then(() => updateGuideCallback()); + this.editorElement.getComponent().getNextUpdatePromise().then(() => updateGuideCallback()); }) ); @@ -50,19 +39,19 @@ module.exports = this.subscriptions.add(this.editor.onDidChangeGrammar(() => { this.configSubscriptions.dispose(); this.handleConfigEvents(); - return updateGuideCallback(); + updateGuideCallback(); }) ); this.subscriptions.add(this.editor.onDidDestroy(() => { this.subscriptions.dispose(); - return this.configSubscriptions.dispose(); + this.configSubscriptions.dispose(); }) ); - return this.subscriptions.add(this.editorElement.onDidAttach(() => { + this.subscriptions.add(this.editorElement.onDidAttach(() => { this.attachToLines(); - return updateGuideCallback(); + updateGuideCallback(); }) ); } @@ -176,8 +165,10 @@ module.exports = const columns = this.getGuidesColumns(this.editor.getPath(), this.editor.getGrammar().scopeName); return (() => { const result = []; - for (let column of Array.from(columns)) { - if (!(column < 0)) { result.push(this.appendGuide(column)); } else { + for (let column of columns) { + if (!(column < 0)) { + result.push(this.appendGuide(column)); + } else { result.push(undefined); } } @@ -193,4 +184,4 @@ module.exports = guide.style.left = `${Math.round(columnWidth)}px`; return this.element.appendChild(guide); } -}); +};