Commit 2867a878 authored by Gaurav Kukreja's avatar Gaurav Kukreja

Alt+Shift+[BNM] for answers without the comments

parent bfd4bba7
...@@ -4,36 +4,10 @@ import requests ...@@ -4,36 +4,10 @@ import requests
import os import os
import re import re
extension_language_dict = {
".py" : "python",
".cpp" : "cpp",
".hpp" : "cpp",
".c" : "c",
".h" : "cpp",
".sh" : "bash",
".css" : "css",
".html" : "html",
".htm" : "html",
".xhtml" : "html",
".jhtml" : "html",
".jsp" : "java",
".jspx" : "java",
".wss" : "java",
".do" : "java",
".action" : "java",
".js" : "javascript",
".pl" : "perl",
".php" : "php",
".php4" : "php",
".php3" : "php",
".phtml" : "php",
".rb" : "ruby",
".swift" : "swift",
}
syntaxFile_language_dict = {"Packages/Python/Python.sublime-syntax": "python"} syntaxFile_language_dict = {"Packages/Python/Python.sublime-syntax": "python"}
def getAnswer(language, query, recommendationNum = 0, withComments = True): def getAnswer(language, query, recommendationNum = 0, withComments = True):
query = re.sub(r"\s+", "+", query)
recommendationStr = "" recommendationStr = ""
if (recommendationNum > 0): if (recommendationNum > 0):
recommendationStr = '/' + str(recommendationNum) recommendationStr = '/' + str(recommendationNum)
...@@ -45,44 +19,43 @@ def getAnswer(language, query, recommendationNum = 0, withComments = True): ...@@ -45,44 +19,43 @@ def getAnswer(language, query, recommendationNum = 0, withComments = True):
class CheatSheetUtils: class CheatSheetUtils:
def error_programming_language_unknown(self): def error_programming_language_unknown(self):
sublime.message_dialog("Cheat.sh plugin could not deduce the programming language.\n\nPlease save the file with an appropriate extension or set the syntax from the Command Palette (Cmd + Shift + p) and typing \"Set Syntax:\"") sublime.message_dialog("Cheat.sh plugin could not deduce the programming language.\n\nPlease save the file with an appropriate extension or set the syntax from the Command Palette by pressing Cmd + Shift + p and typing \"Set Syntax:\"")
def error_unsupported_programming_language(self): def error_unsupported_programming_language(self):
sublime.message_dialog("Cheat.sh plugin does not seem to support this programming language.\n\nPlease contact the developer at gaurav@gauravk.in to include support for this programming language.") sublime.message_dialog("Cheat.sh plugin does not seem to support this programming language.\n\nPlease contact the developer at gaurav@gauravk.in to include support for this programming language.")
def getProgrammingLanguage(self): def getLanguage(self):
fname = self.view.file_name() syntaxFile = self.view.settings().get('syntax')
if fname is not None: m = re.search('Packages/\w*/(?P<language>\w*)\.sublime-syntax', syntaxFile)
filename, fileext = os.path.splitext(self.view.file_name()); if m:
if fileext in extension_language_dict: language = m.group("language")
return extension_language_dict[fileext] return "sublime:" + language.lower()
else:
self.error_unsupported_programming_language()
return
else: else:
syntaxFile = self.view.settings().get('syntax') fname = self.view.file_name()
if syntaxFile in syntaxFile_language_dict: if fname is None:
return syntaxFile_language_dict[syntaxFile] self.error_programming_language_unknown()
m = re.search('/?(?!\w*/)*\w*\.(?P<fileext>\w*)$', fname)
if m:
fileext = m.group("fileext")
return fileext.lower()
else: else:
if syntaxFile == 'Packages/Text/Plain text.tmLanguage': self.error_programming_language_unknown()
self.error_programming_language_unknown()
else:
self.error_unsupported_programming_language()
return None
class CheatSheetCommand(sublime_plugin.TextCommand, CheatSheetUtils): class CheatSheetCommand(sublime_plugin.TextCommand, CheatSheetUtils):
def run(self, edit): def run(self, edit, with_comments):
language = self.getProgrammingLanguage() language = self.getLanguage()
if language is not None: if language is not None:
for region in self.view.sel(): for region in self.view.sel():
if not region.empty(): if not region.empty():
s = self.view.substr(region) query = self.view.substr(region)
s = re.sub(r'\s+(?!\n)', r'+', s) self.view.replace(edit, region, getAnswer(language, query, 0, with_comments))
self.view.replace(edit, region, getAnswer(language, s)) self.view.run_command("reindent")
class CheatSheetMultipleSuggestionsCommand(sublime_plugin.TextCommand, CheatSheetUtils): class CheatSheetMultipleSuggestionsCommand(sublime_plugin.TextCommand, CheatSheetUtils):
show_result_with_comments = True
def on_done(self, user_input): def on_done(self, user_input):
language = self.getProgrammingLanguage() language = self.getLanguage()
if language is not None: if language is not None:
newView = self.view.window().new_file() newView = self.view.window().new_file()
newView.settings().set('auto_indent', False) newView.settings().set('auto_indent', False)
...@@ -91,28 +64,39 @@ class CheatSheetMultipleSuggestionsCommand(sublime_plugin.TextCommand, CheatShee ...@@ -91,28 +64,39 @@ class CheatSheetMultipleSuggestionsCommand(sublime_plugin.TextCommand, CheatShee
sublime.active_window().focus_view(newView) sublime.active_window().focus_view(newView)
separator = '\n\n' + "-"*80 + '\n' + "-"*80 + '\n' + "-"*80 + '\n\n' separator = '\n\n' + "-"*80 + '\n' + "-"*80 + '\n' + "-"*80 + '\n\n'
if sublime.active_window().active_view() == newView: if sublime.active_window().active_view() == newView:
newView.run_command('insert', {"characters": getAnswer(language, user_input)}) newView.run_command('insert', {"characters": getAnswer(language, user_input, 0, self.show_result_with_comments)})
newView.run_command('insert', {"characters": separator}) newView.run_command('insert', {"characters": separator})
newView.run_command('insert', {"characters": getAnswer(language, user_input, 1)}) newView.run_command('insert', {"characters": getAnswer(language, user_input, 1, self.show_result_with_comments)})
newView.run_command('insert', {"characters": separator}) newView.run_command('insert', {"characters": separator})
newView.run_command('insert', {"characters": getAnswer(language, user_input, 2)}) newView.run_command('insert', {"characters": getAnswer(language, user_input, 2, self.show_result_with_comments)})
def run(self, edit): def run(self, edit, with_comments):
self.view.window().show_input_panel("Cheat.sh", "", self.on_done, None, None) self.show_result_with_comments = with_comments
input_panel_prompt = "Cheat.sh"
if with_comments:
input_panel_prompt = input_panel_prompt + " (with comments) :"
else:
input_panel_prompt = input_panel_prompt + " (without comments) :"
self.view.window().show_input_panel(input_panel_prompt, "", self.on_done, None, None)
class CheatSheetInputPanelCommand(sublime_plugin.TextCommand, CheatSheetUtils): class CheatSheetInputPanelCommand(sublime_plugin.TextCommand, CheatSheetUtils):
show_result_with_comments = True
def on_done(self, user_input): def on_done(self, user_input):
language = self.getProgrammingLanguage() language = self.getLanguage()
if language is not None: if language is not None:
old_auto_indent_status = self.view.settings().get('auto_indent') old_auto_indent_status = self.view.settings().get('auto_indent')
if (old_auto_indent_status == True): if (old_auto_indent_status == True):
self.view.settings().set('auto_indent', False) self.view.settings().set('auto_indent', False)
self.view.run_command('insert', {"characters": getAnswer(language, user_input)}) self.view.run_command('insert', {"characters": getAnswer(language, user_input, 0, self.show_result_with_comments)})
if (old_auto_indent_status == True): if (old_auto_indent_status == True):
self.view.settings().set('auto_indent', True) self.view.settings().set('auto_indent', True)
def run(self, edit): def run(self, edit, with_comments):
self.view.window().show_input_panel("Cheat.sh", "", self.on_done, None, None) self.show_result_with_comments = with_comments
input_panel_prompt = "Cheat.sh"
if with_comments:
input_panel_prompt = input_panel_prompt + " (with comments) :"
else:
input_panel_prompt = input_panel_prompt + " (without comments) :"
self.view.window().show_input_panel(input_panel_prompt, "", self.on_done, None, None)
\ No newline at end of file
[ [
{ {
"keys": ["ctrl+shift+b"], "keys": ["ctrl+shift+b"],
"command": "cheat_sheet" "command": "cheat_sheet",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+b"],
"command": "cheat_sheet",
"args":
{
"with_comments": false
}
}, },
{ {
"keys": ["ctrl+shift+n"], "keys": ["ctrl+shift+n"],
"command": "cheat_sheet_multiple_suggestions" "command": "cheat_sheet_multiple_suggestions",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+n"],
"command": "cheat_sheet_multiple_suggestions",
"args":
{
"with_comments": false
}
}, },
{ {
"keys": ["ctrl+shift+m"], "keys": ["ctrl+shift+m"],
"command": "cheat_sheet_input_panel" "command": "cheat_sheet_input_panel",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+m"],
"command": "cheat_sheet_input_panel",
"args":
{
"with_comments": false
}
} }
] ]
\ No newline at end of file
[ [
{ {
"keys": ["super+shift+b"], "keys": ["super+shift+b"],
"command": "cheat_sheet" "command": "cheat_sheet",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+b"],
"command": "cheat_sheet",
"args":
{
"with_comments": false
}
}, },
{ {
"keys": ["super+shift+n"], "keys": ["super+shift+n"],
"command": "cheat_sheet_multiple_suggestions" "command": "cheat_sheet_multiple_suggestions",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+n"],
"command": "cheat_sheet_multiple_suggestions",
"args":
{
"with_comments": false
}
}, },
{ {
"keys": ["super+shift+m"], "keys": ["super+shift+m"],
"command": "cheat_sheet_input_panel" "command": "cheat_sheet_input_panel",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+m"],
"command": "cheat_sheet_input_panel",
"args":
{
"with_comments": false
}
} }
] ]
\ No newline at end of file
[ [
{ {
"keys": ["ctrl+shift+b"], "keys": ["ctrl+shift+b"],
"command": "cheat_sheet" "command": "cheat_sheet",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+b"],
"command": "cheat_sheet",
"args":
{
"with_comments": false
}
}, },
{ {
"keys": ["ctrl+shift+n"], "keys": ["ctrl+shift+n"],
"command": "cheat_sheet_multiple_suggestions" "command": "cheat_sheet_multiple_suggestions",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+n"],
"command": "cheat_sheet_multiple_suggestions",
"args":
{
"with_comments": false
}
}, },
{ {
"keys": ["ctrl+shift+m"], "keys": ["ctrl+shift+m"],
"command": "cheat_sheet_input_panel" "command": "cheat_sheet_input_panel",
"args":
{
"with_comments": true
}
},
{
"keys": ["alt+shift+m"],
"command": "cheat_sheet_input_panel",
"args":
{
"with_comments": false
}
} }
] ]
\ No newline at end of file
[ [
{ "caption": "Cheat.sh: Cheat with comments", "command": "cheat_sheet" }, { "caption": "Cheat.sh: Insert answer with comments", "command": "cheat_sheet_input_panel", "args": {"with_comments": true}},
{ "caption": "Cheat.sh: Cheat pallete with comments", "command": "cheat_sheet_pallete" }, { "caption": "Cheat.sh: Insert answer without comments", "command": "cheat_sheet_input_panel", "args": {"with_comments": false}},
{ "caption": "Cheat.sh: Cheat without comments", "command": "cheat_sheet" } { "caption": "Cheat.sh: Replace query by answer with comments", "command": "cheat_sheet", "args": {"with_comments": true} },
{ "caption": "Cheat.sh: Replace query by answer without comments", "command": "cheat_sheet", "args": {"with_comments": false} },
{ "caption": "Cheat.sh: Show multiple answers with comments", "command": "cheat_sheet_multiple_suggestions", "args": {"with_comments": true} },
{ "caption": "Cheat.sh: Show multiple answers without comments", "command": "cheat_sheet_multiple_suggestions", "args": {"with_comments": false} },
] ]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment