Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to indicate when to automatically show or hide the wrap-guide #780

Merged
merged 5 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
107 changes: 71 additions & 36 deletions packages/wrap-guide/lib/wrap-guide-element.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,143 @@
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) {
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}`});
{scopeSelector: this.editor.getRootScopeDescriptor()});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find where this is allowed, since getRootScopeDescriptor returns a ScopeDescriptor and the scopeSelector keyword option of atom.config.set expects a string. Has this been tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spec file I does the following and it pass without errors:

        const scopeDescriptor = editor.getRootScopeDescriptor();

        atom.config.set('editor.softWrap', false, {scopeSelector: scopeDescriptor});

        expect([atom.config.get('editor.softWrap'),
          atom.config.get('editor.softWrap', {scope: scopeDescriptor})]).toEqual([true, false]);

Not the same config name, but same thing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I don't doubt it works if you pass literally the same object back when you look it up. But if it were being coerced to a string like "[object Object]", that test would pass, and it would be wrong.

Let's change it back just for now and we can figure out what's going on here later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also do some more test since, and the config file is update as normal.
I also tried to pull the wire to see why it works and this is what I found:

  • atom.config.set call setRawScopedValue
  • that call this.scopedSettingsStore.addProperties
    scopedSettingsStore being an object from https://www.npmjs.com/package/scoped-property-store
  • that will call Selector.create
  • that will call slick.parse
  • that will do ('' + expression)
  • that will implicitly call scopeDescriptor.toString
    That return this.getScopeChain()

However, I am not her to force myself her, so will comply and change it back.

}
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) {
atom.config.set('wrap-guide.columns', columns);
if (atom.config.get('wrap-guide.modifyPreferredLineLength')) {
atom.config.set('editor.preferredLineLength', columns[columns.length - 1],
{scopeSelector: `.${this.editor.getGrammar().scopeName}`});
{scopeSelector: this.editor.getRootScopeDescriptor()});
}
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": "If wrapping"
},
{
"value": "atPreferredLineLength",
"description": "If wrapping at preferred line length"
}
],
"default": "always"
}
}
}
Loading