Last active
January 15, 2018 14:05
-
-
Save audunolsen/388d708b806ec9a858f6bd3bcb73e913 to your computer and use it in GitHub Desktop.
A script for Atom which toggles the visibility of the wrap-guide if the preferred line length is exceeded. Will turn into a package at some point if Atom ever gets around to updating their API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### ---------- CLEAN WRAP GUIDE ---------- | |
The ergonomic factors of code readiablity include a preferred line length limit. | |
Automatic soft wrap is a nightmare because it hinders the "write first, refactor later" | |
mentality because it wrecks readability, an unusually long line is just a minor annoyance. | |
A wrap guide is a visual cue showing you if cleanup is necessary at a later point. | |
The problem: having a wrap guide visible at all times is distracting visual noise. | |
This script toggles the visibility of the wrap guide if a line exceeds the preferred line length. | |
NOTE: This functionality was intended to be published in a separate package. | |
Because atom's API is lackluster, this functionality is currently only possible | |
in conjunction with the "zentabs" package where the tab limit is set to 1. | |
config.cson for the lazy ⬇ | |
zentabs: | |
maximumOpenedTabs: 1 | |
showPinnedIcon: false | |
This is because the TextEditor object returned has zero common data with | |
the corresponding DOM element, but ironaically the parent <atom-pane> element has (!?) | |
I've published an isse on their Github repo about this: | |
https://github.com/atom/atom/issues/16434 | |
### | |
EVENT = "toggleWrapGuide" | |
toggleWrapGuide = (editor) -> | |
lines = editor.getText().split '\n' | |
length = atom.config.settings.editor.preferredLineLength or 80 | |
filePath = editor.getPath() | |
wrapGuide = document.querySelector "atom-pane[data-active-item-path=\"#{filePath}\"] | |
.wrap-guide" | |
exceeded = if (lines.every (line) -> line.length < length) then false else true | |
# console.log if exceeded then "Show guide" else "Hide guide" | |
if wrapGuide | |
wrapGuide.style.opacity = if exceeded then 1 else 0 | |
wrapGuideEventExists = (editor) -> | |
eventExist = false | |
for event in getChangeEvents(editor) | |
if event.eventName is EVENT then eventExist = true | |
return eventExist | |
addWrapGuideEvent = (editor) -> | |
editor.onDidChange -> toggleWrapGuide editor | |
changeEvents = getChangeEvents(editor) | |
changeEvents[changeEvents.length - 1].eventName = EVENT | |
getChangeEvents = (editor) -> | |
changeEvents = editor.emitter.handlersByEventName["did-change"] | |
if changeEvents then return changeEvents else return [] | |
toggleWrapGuide editor for editor in atom.workspace.getTextEditors() | |
activePane = atom.workspace.getActivePaneItem() | |
if activePane then addWrapGuideEvent activePane | |
atom.workspace.onDidChangeActivePaneItem (editor) -> | |
# Quit if pane opened isnt a text editor (e.g. settings pane or left) | |
if not editor or editor.constructor.name isnt "TextEditor" then return | |
# Do once when changing pane | |
toggleWrapGuide editor | |
# On text-buffer change | |
if !wrapGuideEventExists editor then addWrapGuideEvent editor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.wrap-guide { | |
height: 100%; | |
width: 0px; | |
background-image: linear-gradient(to bottom, fade(@alert, 50%) 50%, @workspace-bg 0%); | |
background-position: left; | |
background-size: 1px 10px; | |
background-repeat: repeat-y; | |
opacity: 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: This is now it's own package! Due to skimming of the documentation on atom.io I missed a class function doing exactly what I needed, but it now works as intended. There's still additional functionality to add, but the base functionality to enhance the wrap-guide is now present in its own package.