Skip to content

Commit

Permalink
Add rewrap.autoWrap.enabled setting (#87)
Browse files Browse the repository at this point in the history
Adds a new setting: `rewrap.autoWrap.enabled` (boolean). To keep things
  simpler for now, it has `application` scope, meaning it can't be used
  in workspace or workspace folder settings files.

For now I've also kept "hidden" internal setting to keep track of on/off
state. How it works is like this:
1. If no '...enabled' setting exists in the user's settings file, the
   internal flag is still used as before. Using the toggle command will
   alter this flag.
2. If the '...enabled' setting *is* present in the user's setting's file
   (set either true or false), this takes precedence. If the toggle
   command is then used, it modifies the setting in the settings file
   instead.

This can be changed in the future without too much impact. 2 ways to go
on this: always store the on/off state in the setting; or use the
setting just to set the state at startup (or if it's changed): the
toggle would just set an extra flag to turn it off temporarily. Another
option would be to make the toggle command invisible altogether if the
feature is disabled in the settings.
  • Loading branch information
stkb committed May 16, 2018
1 parent f8c4c65 commit 2c927ab
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
See also [https://github.com/stkb/vscode-rewrap/releases](https://github.com/stkb/vscode-rewrap/releases) (for working links to issues)


## Unreleased

The new setting `rewrap.autoWrap.enabled` can now be used to ensure this feature
is enabled in new installations (#87). Apart from that, the feature and the
toggle command for it work the same as before.


## 1.9.0

- Support East Asian (CJK) languages (#75)
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
"type": "boolean",
"default": false,
"description": "(EXPERIMEMTAL) When wrapping lines, reformat paragraph indents."
},
"rewrap.autoWrap.enabled": {
"scope": "application",
"type": "boolean",
"default": false,
"description": "Wraps automatically when the cursor is past the wrapping column."
}
}
},
Expand Down
62 changes: 39 additions & 23 deletions vscode/Extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const { DocState, Position, Selection } = require('./compiled/Core/Types')
const Rewrap = require('./compiled/Core/Main')

/** Function to activate the extension. */
exports.activate = function activate(context)
exports.activate = async function activate(context)
{
const toggleAutoWrapCommand =
await initAutoWrap (workspace, context.globalState, window)

// Register the commands
context.subscriptions.push
( commands.registerTextEditorCommand
Expand Down Expand Up @@ -56,28 +59,6 @@ exports.activate = function activate(context)
doWrap(editor, customColumn)
}
}

let changeHook
if(context.globalState.get('autoWrap')) {
toggleAutoWrapCommand();
}

/** Auto-wrap automatically wraps the current line when space or enter is
* pressed after the wrapping column. */
function toggleAutoWrapCommand()
{
if(changeHook) {
changeHook.dispose()
changeHook = null
context.globalState.update('autoWrap', false)
.then(() => window.setStatusBarMessage("Auto-wrap: Off", 7000))
}
else {
changeHook = workspace.onDidChangeTextDocument(checkChange)
context.globalState.update('autoWrap', true)
.then(() => window.setStatusBarMessage("Auto-wrap: On", 7000))
}
}
}

/** Catches any error and displays a friendly message to the user. */
Expand Down Expand Up @@ -190,6 +171,8 @@ const doWrap = (editor, customColumn) => {
catch(err) { catchErr(err) }
}

/********** Auto-Wrap **********/

const checkChange = e => {
// Make sure we're in the active document
const editor = window.activeTextEditor
Expand Down Expand Up @@ -224,3 +207,36 @@ const checkChange = e => {
}
catch(err) { catchErr(err) }
}

const initAutoWrap = async (workspace, extState, window) => {
const config = workspace.getConfiguration('rewrap.autoWrap')
const getEnabledSetting = () => config.inspect('enabled').globalValue
const getEnabled = () => { // can still return null/undefined
const s = getEnabledSetting ()
return s !== undefined ? s : extState.get('autoWrap')
}

let changeHook
const setEnabled = async (on) => {
if (on && !changeHook)
changeHook = workspace.onDidChangeTextDocument(checkChange)
else if (!on && changeHook) {
changeHook.dispose()
changeHook = null
}
const s = getEnabledSetting ()
if (s === undefined) await extState.update('autoWrap', on)
else {
await extState.update('autoWrap', null)
if (s !== on) await config.update('enabled', on, true)
}
await window.setStatusBarMessage(`Auto-wrap: ${on?'On':'Off'}`, 7000)
}

const checkState = async () => setEnabled (getEnabled ())
workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('rewrap.autoWrap')) checkState ()
})
await checkState ()
return async () => { setEnabled (!getEnabled()) }
}

0 comments on commit 2c927ab

Please sign in to comment.