Created
October 2, 2012 18:58
-
-
Save stibi/3822481 to your computer and use it in GitHub Desktop.
A small Sublime Text 2 plugin for C/C++ developers. It opens for you a header file for the current file and vice versa.
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
import os | |
import sublime_plugin | |
class ChelpCommand(sublime_plugin.WindowCommand): | |
ALLOWED_EXT = (".c", ".h") | |
def swap_extension(self, current_extension): | |
if current_extension == ".c": | |
return "h" | |
return "c" | |
def construct_path(self, filename, extension): | |
# under filename is the full absolute path to the file, without extension | |
_exploded_file_path = filename.split("/") | |
# name of the file, without extension | |
file_name = _exploded_file_path[-1] | |
# and this is just a path to the file, without filename, just a absolute path | |
file_path = "/".join(_exploded_file_path[:-1]) | |
return "%s/%s.%s" % (file_path, file_name, extension) | |
def run(self, *args, **kwargs): | |
view = self.window.active_view() | |
# file_name = absolute path to the file, without extension | |
file_name, file_extension = os.path.splitext(view.file_name()) | |
if not file_extension in self.ALLOWED_EXT: | |
print "I can't help you with this file extension, bye." | |
return | |
new_extension = self.swap_extension(file_extension) | |
path = self.construct_path(file_name, new_extension) | |
self.window.open_file(path) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment