Skip to content

Instantly share code, notes, and snippets.

@jamesmacaulay
Last active January 18, 2021 19:01
Show Gist options
  • Save jamesmacaulay/5457344 to your computer and use it in GitHub Desktop.
Save jamesmacaulay/5457344 to your computer and use it in GitHub Desktop.
Clojure stuff for Sublime Text 2. Files live in ~/Application Support/Sublime Text 2/Packages/User
// installed Clojure packages:
//
// * BracketHighlighter
// * lispindent
// * SublimeREPL
// * sublime-paredit
{
"word_separators": "/\\()\"',;!@$%^&|+=[]{}`~?",
"paredit_enabled": true,
"paredit_dont_match_single_quotes": true
}
import re, sublime, sublime_plugin, sublimerepl, text_transfer
class RefreshNamespacesInReplCommand(text_transfer.ReplTransferCurrent):
def run(self, edit):
text = "(clojure.tools.namespace.repl/refresh)"
self.view.window().run_command("repl_send", {"external_id": self.repl_external_id(), "text": text, "file_name": self.view.file_name()})
class SwitchToCurrentNamespaceInReplCommand(text_transfer.ReplTransferCurrent):
def run(self, edit):
if sublimerepl.manager.repl_view(self.view):
return
ns = re.sub("ns\s*", "", self.view.substr(self.view.find("ns\s*\S+",0)))
text = "(in-ns '" + ns + ")"
self.view.window().run_command("repl_send", {"external_id": self.repl_external_id(), "text": text, "file_name": self.view.file_name()})
class RunAllClojureTestsFromProjectInReplCommand(text_transfer.ReplTransferCurrent):
def run(self, edit):
form = "(do (clojure.tools.namespace.repl/refresh) (apply clojure.test/run-tests (clojure.tools.namespace.find/find-namespaces-in-dir (clojure.java.io/file \"test\"))))"
self.view.window().run_command("refresh_namespaces_in_repl")
self.view.window().run_command("repl_send", {"external_id": self.repl_external_id(), "text": form, "file_name": self.view.file_name()})
class RunClojureTestsFromCurrentNamespaceInReplCommand(text_transfer.ReplTransferCurrent):
def run(self, edit):
if sublimerepl.manager.repl_view(self.view):
return
ns = re.sub("ns\s*", "", self.view.substr(self.view.find("ns\s*\S+",0)))
default_test_ns = re.sub("(.*)(?<!-test)\\Z","\\1-test", ns, 1)
alt_style_test_ns = re.sub("\A([^\\.]*\\.)(?!test)","\\1test.", ns, 1)
form = "(try (clojure.test/run-tests '" + default_test_ns + ")\n (catch Exception e\n (clojure.test/run-tests '" + alt_style_test_ns + ")))"
self.view.window().run_command("refresh_namespaces_in_repl")
self.view.window().run_command("repl_send", {"external_id": self.repl_external_id(), "text": form, "file_name": self.view.file_name()})
[
{ "keys": ["escape"], "command": "auto_complete", "context":
[
{ "key": "auto_complete_visible", "operator": "equal", "operand": false },
{ "key": "overlay_visible", "operator": "equal", "operand": false },
{ "key": "panel_visible", "operator": "equal", "operand": false },
{ "key": "num_selections", "operator": "equal", "operand": 1 }
]
},
{ "keys": ["super+shift+t"], "command": "reopen_last_file" },
{ "keys": ["ctrl+super+r"], "command": "reveal_in_side_bar" },
// SublimeREPL bindings:
// =====================
{ "keys": ["alt+super+b"], "command": "repl_transfer_current", "args": {"scope": "block"}},
{ "keys": ["alt+super+r"], "command": "refresh_namespaces_in_repl"},
{ "keys": ["alt+super+i"], "command": "switch_to_current_namespace_in_repl"},
{ "keys": ["alt+super+x"], "command": "run_clojure_tests_from_current_namespace_in_repl"},
{ "keys": ["alt+super+a"], "command": "run_all_clojure_tests_from_project_in_repl"},
// BracketHighlighter bindings:
// ===========================
// Swap bracket type
{ "keys": ["ctrl+shift+]"],
"command": "swap_brackets"},
// Swap quotes (only goes boths ways in supported languages...)
{ "keys": ["ctrl+shift+'"],
"command": "bh_key", "args": {"lines" : true, "plugin": {"type": ["single_quote", "double_quote", "py_single_quote", "py_double_quote"], "command": "bh_modules.swapquotes"} }},
// Select text between brackets
{ "keys": ["ctrl+alt+a"],
"command": "bh_key", "args": {"lines" : true, "plugin": {"type": ["__all__"], "command": "bh_modules.bracketselect"} } },
// Select tag name of HTML/XML tag (both opening name and closing)
{ "keys": ["ctrl+alt+t"],
"command": "bh_key", "args": {"plugin": {"type": ["cfml", "html", "angle"], "command": "bh_modules.tagnameselect"} } },
// Toggle high visibility mode
{ "keys": ["alt+super+="],
"command": "bh_toggle_high_visibility"},
// sublime-paredit fixes:
// ======================
// Don't constrain close-parens unless we're in paredit mode:
{ "keys": [")"], "command": "insert_snippet", "args": {"contents": ")"}, "context":
[{ "key": "setting.paredit_enabled", "operator": "equal", "operand": false }]},
{ "keys": [")"], "command": "run_macro_file", "args": {"file": "Packages/sublime-paredit/Paredit Closing Bracket.sublime-macro_"}, "context":
[{ "key": "setting.paredit_enabled", "operator": "equal", "operand": true }]},
{ "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
]
}
]
// If you need to customize the SublimeREPL settings for Clojure, you can copy Main.sublime-menu
// from Packages/SublimeREPL/config/Clojure into Packages/User and edit as necessary. This will
// change the behaviour of the REPL when selected from the menu (Tools > SublimeREPL > Clojure >
// Clojure) but if you launch the REPL from the default shortcut or the command palette, it will
// use the original unedited settings from the package.
// This is the unedited file from the version of SublimeREPL I have right now:
[
{
"id": "tools",
"children":
[{
"caption": "SublimeREPL",
"mnemonic": "r",
"id": "SublimeREPL",
"children":
[
{"caption": "Clojure",
"id": "Clojure",
"children":[
{"command": "repl_open",
"caption": "Clojure",
"id": "repl_clojure",
"args": {
"type": "subprocess",
"encoding": "utf8",
"cmd": {"windows": ["lein.bat", "repl"],
"linux": ["lein", "repl"],
"osx": ["lein", "repl"]},
"soft_quit": "\n(. System exit 0)\n",
"cwd": {"windows":"c:/Clojure", // where the lein.bat lives!
"linux": "$file_path",
"osx": "$file_path"},
"syntax": "Packages/Clojure/Clojure.tmLanguage",
"external_id": "clojure",
"extend_env": {"INSIDE_EMACS": "1"}
}
},
{"command": "clojure_auto_telnet_repl",
"id": "repl_clojure_telnet",
"caption": "Clojure-Telnet"}]}
]
}]
}
]
{
"theme": "Flatland.sublime-theme",
"color_scheme": "Packages/Theme - Flatland/Flatland.tmTheme",
"font_face": "Inconsolata",
"font_size": 18.5,
"caret_style": "wide",
"highlight_line": true,
"match_brackets": false, // otherwise it gets confusing with BracketHighlighter installed
"draw_indent_guides": true,
"ignored_packages": ["Vintage", "PHP"],
"tab_size": 2,
"detect_indentation": false,
"translate_tabs_to_spaces": true
}
{
"default_extend_env": {"PATH": "/opt/boxen/homebrew/bin:{PATH}:/usr/local/bin"}
}
@tbrooke
Copy link

tbrooke commented Dec 7, 2013

I tried ClojureHelpers.py in Sublime Text 3 and coudln't get it to work - I was unsure of the location for the file but I put them in:

/SublimeREPL/config/Clojure/ClojureHelpers.py

I haven't tried removing the file name parameters as mentioned above

@jasongilman
Copy link

I've documented my Sublime Text 2 setup based on this gist. I added a couple functions to ClojureHelpers.py and more recommended key combinations https://github.com/jasongilman/SublimeClojureSetup

@michaelphines
Copy link

@tbrooke I ported it to ST3 here: https://gist.github.com/michaelphines/8818209

The file goes in ~/Application Support/Sublime Text 2/Packages/User

@xpe
Copy link

xpe commented Nov 12, 2014

@jamesmacaulay Thanks for sharing. What drove your changes to word_separators? I'm guessing you based in on the rules for symbols mentioned at http://clojure.org/reader, is that right?

@lordB8r
Copy link

lordB8r commented Jan 6, 2015

@michaelphines & @tbrooke - I have it installed at

~/Library/Application Support/Sublime Text 3/Packages/User

@judodan
Copy link

judodan commented Jan 28, 2015

For the life of me, I can't get this to work at all. I even changed to using @jasongilman update to ClojureHelpers.py but to no avail. Has everybody else just dropped this in and it worked?

@jlroo
Copy link

jlroo commented Feb 3, 2015

FYI : For the preference if you are using Flatland it should be:

"theme": "Flatland Dark.sublime-theme",
"color_scheme": "Packages/Theme - Flatland/Flatland Dark.tmTheme"

@kirked
Copy link

kirked commented Mar 19, 2020

Everywhere this is used to get the namespace,

ns = re.sub("ns\s*", "", self.view.substr(self.view.find("ns\s*\S+",0)))

re.sub needs the additional count parameter of 1, otherwise it can munge your namespace if it contains "ns" (as in x.transforms).

Fixed:

ns = re.sub("ns\s*", "", self.view.substr(self.view.find("ns\s*\S+",0)), 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment