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

Adds selected comment folding functionality #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[
{ "keys": ["ctrl+shift+c"], "command": "toggle_fold_comments" }
{ "keys": ["ctrl+shift+c"], "command": "toggle_fold_comments" },
{ "keys": ["ctrl+alt+c"], "command": "toggle_fold_selected_comments" }
]
3 changes: 2 additions & 1 deletion Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[
{ "keys": ["super+shift+c"], "command": "toggle_fold_comments" }
{ "keys": ["super+shift+c"], "command": "toggle_fold_comments" },
{ "keys": ["super+alt+c"], "command": "toggle_fold_selected_comments" }
]
3 changes: 2 additions & 1 deletion Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[
{ "keys": ["ctrl+shift+c"], "command": "toggle_fold_comments" }
{ "keys": ["ctrl+shift+c"], "command": "toggle_fold_comments" },
{ "keys": ["ctrl+alt+c"], "command": "toggle_fold_selected_comments" }
]
4 changes: 4 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
{
"caption": "Toggle Fold Comments",
"command": "toggle_fold_comments"
},
{
"caption": "Toggle Fold Selected Comments",
"command": "toggle_fold_selected_comments"
}
]
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Sublime Text 2/3 plugin for folding (hiding) comments

![Toggle Comment Folding](https://raw.github.com/oskarols/foldcomments/master/foldcomments.gif)

As of version 0.2.0 there is also an option of folding only the selected comments! In order to do this ctrl+click as many comments as you would like to fold. Then press the correct keys (as shown in the keybinds section).

## Installation

Expand All @@ -16,16 +17,18 @@ You can install via [Sublime Package Manager](https://sublime.wbond.net/) where

###### OSX
Toggle Comments Folding: <kbd>Command</kbd> + <kbd>Shift</kbd> + <kbd>C</kbd>
Toggle Selected Comments Folding: <kbd>Command</kbd> + <kbd>Alt</kbd> + <kbd>C</kbd>

###### Windows/Linux
Toggle Comments Folding: <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>C</kbd>

Toggle Selected Comments Folding: <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>C</kbd>

#### Commands

* `Toggle Folding Comments`
* `Fold Comments`
* `Unfold Comments`
* `Toggle Folding Selected Comments`


## Credits
Expand Down
33 changes: 25 additions & 8 deletions foldcomments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sublime import Region, Settings, load_settings
from sublime import Region, Settings, load_settings, Selection
import sublime_plugin

from itertools import tee, chain
Expand Down Expand Up @@ -105,17 +105,26 @@ def normalize_multiline_comment(view, region):
class CommentNodes:

def __init__(self, view):
self.comments = None # collection of Region objects
self.comments = [] # collection of Region objects
self.settings = load_settings("foldcomments.sublime-settings")
self.view = view
self.find_comments()
self.apply_settings()

def find_comments(self):
self.comments = [
normalize_comment(self.view, c) for c in self.view.find_by_selector('comment')
]

self.apply_settings()

def find_selected_comments(self):
self.selectedComments = [
normalize_comment(self.view, c) for c in self.view.find_by_selector('comment')
]
for c in self.selectedComments:
for sel in self.view.sel():
if c.contains(sel.begin()):
self.comments.append(c)
self.apply_settings()

def apply_settings(self):
if not self.settings.get('fold_single_line_comments'):
self.remove_single_line_comments()
Expand Down Expand Up @@ -160,28 +169,36 @@ def unfold(self):
def toggle_folding(self):
def is_folded(comments):
return self.view.unfold(comments[0]) # False if /already folded/

self.unfold() if is_folded(self.comments) else self.fold()

self.unfold() if is_folded(self.comments) else self.fold()

# ================================= COMMANDS ==================================

class ToggleFoldCommentsCommand(sublime_plugin.TextCommand):

def run(self, edit):
comments = CommentNodes(self.view)
comments.find_comments()
comments.toggle_folding()


class FoldCommentsCommand(sublime_plugin.TextCommand):

def run(self, edit):
comments = CommentNodes(self.view)
comments.find_comments()
comments.fold()


class UnfoldCommentsCommand(sublime_plugin.TextCommand):

def run(self, edit):
comments = CommentNodes(self.view)
comments.find_comments()
comments.unfold()

class ToggleFoldSelectedCommentsCommand(sublime_plugin.TextCommand):

def run(self, edit):
comments = CommentNodes(self.view)
comments.find_selected_comments()
comments.toggle_folding()
1 change: 1 addition & 0 deletions package-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"description": "Sublime Text 2/3 plugin for folding (hiding) comments", "sublime_text": "*", "platforms": ["*"], "url": "https://github.com/oskarols/foldcomments", "version": "0.2.0", "dependencies": []}