diff --git a/CHANGELOG.md b/CHANGELOG.md index c4a18e9..72665e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/package.json b/package.json index 9dc3e38..8cfd9ec 100644 --- a/package.json +++ b/package.json @@ -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." } } }, diff --git a/vscode/Extension.js b/vscode/Extension.js index 48829bd..30c7c43 100644 --- a/vscode/Extension.js +++ b/vscode/Extension.js @@ -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 @@ -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. */ @@ -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 @@ -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()) } +}