Skip to content

Commit

Permalink
Merge pull request #780 from Trigan2025/master
Browse files Browse the repository at this point in the history
Ability to indicate when to automatically show or hide the wrap-guide
  • Loading branch information
DeeDeeG authored Nov 16, 2023
2 parents 580dfbb + e2be9ad commit fbd2b24
Show file tree
Hide file tree
Showing 5 changed files with 617 additions and 266 deletions.
19 changes: 12 additions & 7 deletions packages/wrap-guide/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ module.exports = {
this.subscriptions = new CompositeDisposable();
this.wrapGuides = new Map();

this.subscriptions.add(atom.workspace.observeTextEditors(editor => {
if (this.wrapGuides.has(editor)) { return; }
this.when = atom.config.get("wrap-guide.showWrapGuide");
this.subscriptions.add(atom.config.onDidChange('wrap-guide.showWrapGuide', (args) => {
this.when = args.newValue;
atom.workspace.getTextEditors().forEach(async (editor) => {
await this.wrapGuides.get(editor).setWhen(this.when);
});
}));

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);
const wrapGuideElement = new WrapGuideElement(editor, editorElement, this.when);

this.wrapGuides.set(editor, wrapGuideElement);
this.subscriptions.add(editor.onDidDestroy(() => {
this.wrapGuides.get(editor).destroy();
this.wrapGuides.delete(editor);
})
);
})
);
}));
}));
},

deactivate() {
Expand Down
103 changes: 69 additions & 34 deletions packages/wrap-guide/lib/wrap-guide-element.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,103 @@
const {CompositeDisposable} = require('atom');

module.exports = class WrapGuideElement {
constructor(editor, editorElement) {
#_when = null;
#_shouldShow = null;
constructor(editor, editorElement, when) {
this.editor = editor;
this.editorElement = editorElement;
this.subscriptions = new CompositeDisposable();
this.configSubscriptions = new CompositeDisposable();
this.softWrapAPLLsubscriptions = null;
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.setWhen(when);

this.element.updateGuide = this.updateGuide.bind(this);
this.element.updateGuide = (async () => await this.updateGuide()).bind(this);
this.element.getDefaultColumn = this.getDefaultColumn.bind(this);
}

get shouldShow() { return this.#_shouldShow; }
get when() { return this.#_when; }

setWhen(when) {
if (when == this.when) return;
this.#_when = when;
this.updateWhen();
}

async updateWhen() {
switch (this.when) {
case "atPreferredLineLength":
this.#_shouldShow = this.editor.isSoftWrapped() && atom.config.get('editor.softWrapAtPreferredLineLength', {scope: this.editor.getRootScopeDescriptor()});
break;
case "wrapping":
this.#_shouldShow = this.editor.isSoftWrapped();
break;
default: // "always"
this.#_shouldShow = true;
break;
}
await this.updateGuide();
}

attachToLines() {
const scrollView = this.editorElement.querySelector('.scroll-view');
return (scrollView != null ? scrollView.appendChild(this.element) : undefined);
}

handleEvents() {
const updateGuideCallback = () => this.updateGuide();
const updateGuideCallback = async () => await this.updateGuide();

this.handleConfigEvents();

this.subscriptions.add(atom.config.onDidChange('editor.fontSize', () => {
this.subscriptions.add(this.editor.onDidChangeSoftWrapped(async (wrapped) => {
if (this.when === null) return;
await this.updateWhen();
}));

this.subscriptions.add(atom.config.onDidChange('editor.fontSize', async () => {
// Wait for editor to finish updating before updating wrap guide
// TODO: Use async/await once this file is converted to JS
this.editorElement.getComponent().getNextUpdatePromise().then(() => updateGuideCallback());
})
);
await this.editorElement.getComponent().getNextUpdatePromise();
updateGuideCallback();
}));

this.subscriptions.add(this.editorElement.onDidChangeScrollLeft(updateGuideCallback));
this.subscriptions.add(this.editor.onDidChangePath(updateGuideCallback));
this.subscriptions.add(this.editor.onDidChangeGrammar(() => {
this.subscriptions.add(this.editor.onDidChangeGrammar(async () => {
this.configSubscriptions.dispose();
this.handleConfigEvents();
updateGuideCallback();
})
);
await this.updateWhen();
}));

this.subscriptions.add(this.editor.onDidDestroy(() => {
this.subscriptions.dispose();
if (this.softWrapAPLLsubscriptions) this.softWrapAPLLsubscriptions.dispose();
this.configSubscriptions.dispose();
})
);
}));

this.subscriptions.add(this.editorElement.onDidAttach(() => {
this.subscriptions.add(this.editorElement.onDidAttach(async () => {
this.attachToLines();
updateGuideCallback();
})
);
await updateGuideCallback();
}));
}

handleConfigEvents() {
const {uniqueAscending} = require('./main');

const updatePreferredLineLengthCallback = args => {
if (this.softWrapAPLLsubscriptions) this.softWrapAPLLsubscriptions.dispose();
this.softWrapAPLLsubscriptions = new CompositeDisposable();

this.softWrapAPLLsubscriptions.add(atom.config.onDidChange('editor.softWrapAtPreferredLineLength',
{scope: this.editor.getRootScopeDescriptor()}, async ({newValue}) => {
if (this.when === null) return;
await this.updateWhen();
}));

const updatePreferredLineLengthCallback = async (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) {
Expand All @@ -68,24 +106,22 @@ module.exports = class WrapGuideElement {
atom.config.set('wrap-guide.columns', columns,
{scopeSelector: `.${this.editor.getGrammar().scopeName}`});
}
return this.updateGuide();
return await this.updateGuide();
};
this.configSubscriptions.add(atom.config.onDidChange(
'editor.preferredLineLength',
{scope: this.editor.getRootScopeDescriptor()},
updatePreferredLineLengthCallback
)
);
));

const updateGuideCallback = () => this.updateGuide();
const updateGuideCallback = async () => await this.updateGuide();
this.configSubscriptions.add(atom.config.onDidChange(
'wrap-guide.enabled',
{scope: this.editor.getRootScopeDescriptor()},
updateGuideCallback
)
);
));

const updateGuidesCallback = args => {
const updateGuidesCallback = async (args) => {
// ensure that multiple guides stay sorted in ascending order
const columns = uniqueAscending(args.newValue);
if (columns != null ? columns.length : undefined) {
Expand All @@ -94,15 +130,14 @@ module.exports = class WrapGuideElement {
atom.config.set('editor.preferredLineLength', columns[columns.length - 1],
{scopeSelector: `.${this.editor.getGrammar().scopeName}`});
}
return this.updateGuide();
return await this.updateGuide();
}
};
return this.configSubscriptions.add(atom.config.onDidChange(
'wrap-guide.columns',
{scope: this.editor.getRootScopeDescriptor()},
updateGuidesCallback
)
);
));
}

getDefaultColumn() {
Expand Down Expand Up @@ -130,15 +165,14 @@ module.exports = class WrapGuideElement {
}

updateGuide() {
if (this.isEnabled()) {
if (this.isEnabled())
return this.updateGuides();
} else {
return this.hide();
}
else return this.hide();
}

updateGuides() {
this.removeGuides();
if (!this.shouldShow) return this.hide();
this.appendGuides();
if (this.element.children.length) {
return this.show();
Expand All @@ -150,6 +184,7 @@ module.exports = class WrapGuideElement {
destroy() {
this.element.remove();
this.subscriptions.dispose();
if (this.softWrapAPLLsubscriptions) this.softWrapAPLLsubscriptions.dispose();
return this.configSubscriptions.dispose();
}

Expand Down
21 changes: 20 additions & 1 deletion packages/wrap-guide/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wrap-guide",
"version": "0.41.0",
"version": "0.41.1",
"main": "./lib/main",
"description": "Displays a vertical line at the 80th character in the editor.\nThis packages uses the config value of `editor.preferredLineLength` when set.",
"license": "MIT",
Expand All @@ -25,6 +25,25 @@
"enabled": {
"default": true,
"type": "boolean"
},
"showWrapGuide": {
"type": "string",
"description": "Choose when to show the wrap guide.",
"enum": [
{
"value": "always",
"description": "Always"
},
{
"value": "wrapping",
"description": "When soft wrap is enabled"
},
{
"value": "atPreferredLineLength",
"description": "When soft wrap at preferred line length is enabled"
}
],
"default": "always"
}
}
}
Loading

0 comments on commit fbd2b24

Please sign in to comment.