-
Notifications
You must be signed in to change notification settings - Fork 93
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
some changes to make it work like Emacs vertical file completion. #185
base: master
Are you sure you want to change the base?
Changes from 1 commit
c6c13fd
988b8a5
9066872
c4dcae7
26a17b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,6 @@ | |
|
||
|
||
class AdvancedNewFileBase(object): | ||
static_input_panel_view = None | ||
|
||
def __init__(self, window): | ||
super() | ||
self.window = window | ||
|
@@ -270,6 +268,38 @@ def __translate_alias(self, path): | |
def input_panel_caption(self): | ||
return "" | ||
|
||
def get_active_window_settings(self): | ||
return sublime.active_window().settings() | ||
|
||
def set_input_view(self, input_view): | ||
self.get_active_window_settings().set("anf_input_view", input_view.id()) | ||
|
||
def get_input_view(self): | ||
return sublime.View(self.get_active_window_settings().get("anf_input_view")) | ||
|
||
|
||
def get_active_view_settings(self): | ||
view = self.get_input_view() | ||
if view: | ||
return view.settings() | ||
else: | ||
return self.get_active_window_settings() | ||
|
||
def clear_input_view(self): | ||
self.get_active_view_settings().erase("anf_input_view") | ||
|
||
def set_input_view_content(self, content): | ||
self.get_active_view_settings().set("anf_input_view_content", content) | ||
|
||
def get_input_view_content(self): | ||
return self.get_active_view_settings().get("anf_input_view_content") | ||
|
||
def clear_input_view_content(self): | ||
self.get_active_view_settings().erase("anf_input_view_content") | ||
|
||
def clear_input_view_project_files(self): | ||
self.get_active_view_settings().erase("anf_input_view_project_files") | ||
Comment on lines
+289
to
+302
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imo these functions provide barely any value over their bodies. Besides, I'm unsure what the purpose of If you want to store data per window, you should probably do that explicitly (and all the time instead of only sometimes). However, I'm unsure if a window's settings are persisted on disk on exit, so this may actually result in leaking outdated data. An alternative would be a separate mapping that associates window-specific data with the window's id and is sufficiently cleared when the panel is closed. Usually I'd suggest storing that in the window command's instance variables, but since this plugin uses multiple commands that's not really an option. |
||
|
||
def show_filename_input(self, initial): | ||
caption = self.input_panel_caption() | ||
|
||
|
@@ -286,8 +316,8 @@ def show_filename_input(self, initial): | |
self.input_panel_view.settings().set("anf_panel", True) | ||
if self.settings.get(CURSOR_BEFORE_EXTENSION_SETTING): | ||
self.__place_cursor_before_extension(self.input_panel_view) | ||
AdvancedNewFileBase.static_input_panel_view = self.input_panel_view | ||
self.__update_filename_input('') | ||
self.set_input_view(self.input_panel_view) | ||
self.__update_filename_input(initial) | ||
|
||
def __update_filename_input(self, path_in): | ||
new_content = path_in | ||
|
@@ -296,9 +326,9 @@ def __update_filename_input(self, path_in): | |
if self.view is not None: | ||
self.view.erase_status("AdvancedNewFile2") | ||
|
||
input_view = AdvancedNewFileBase.static_input_panel_view | ||
input_view = self.get_input_view() | ||
if path_in.endswith("\t"): | ||
creation_path, candidate, completion_list = self.parse_status_line(self.get_status_line()) # type: ignore | ||
creation_path, candidate, completion_list = self.get_input_view_content() | ||
new_content = self.completion_input(path_in.replace("\n", "").replace("\t", ""), candidate) | ||
elif path_in.endswith("\n"): | ||
path_in = path_in.replace("\n", "") | ||
|
@@ -312,25 +342,41 @@ def __update_filename_input(self, path_in): | |
self.window.run_command("hide_panel", {"cancel": True}) | ||
return | ||
else: | ||
completion_list = self.completion.hint(path_in) | ||
completion_list = self.get_completion_list(path_in) | ||
if completion_list: | ||
candidate = completion_list[0] | ||
completion_list.remove(candidate) | ||
else: | ||
candidate = '' | ||
|
||
|
||
if input_view: | ||
input_view.hide_popup() | ||
try: | ||
input_view.hide_popup() | ||
except Exception as e: | ||
print("hide_popup", e) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug print? |
||
if input_view and new_content != path_in: | ||
input_view.run_command("anf_replace", {"content": new_content}) | ||
else: | ||
base, path = self.split_path(path_in) | ||
status_line = generate_creation_path(self.settings, base, path, True) + '|' + candidate + str(completion_list) | ||
creation_path = generate_creation_path(self.settings, base, path, True) | ||
status_line = self.create_status_line(creation_path, candidate, completion_list) | ||
self.set_input_view_content((creation_path, candidate, completion_list)) | ||
|
||
if self.settings.get(SHOW_PATH_SETTING, False): | ||
self.update_status_message(status_line) | ||
if input_view and candidate and not new_content.endswith(candidate): | ||
input_view.show_popup('<strong>' + candidate + '</strong><br/>' + '<br/>'.join(completion_list)) | ||
if not new_content.endswith(candidate): | ||
self.show_input_popup(candidate, completion_list) | ||
|
||
def show_input_popup(self, candidate, completion_list): | ||
try: | ||
input_view = self.get_input_view() | ||
if input_view and candidate: | ||
input_view.show_popup('<strong>' + candidate + '</strong><br/>' + '<br/>'.join(completion_list), max_width=1024) | ||
except Exception as e: | ||
print("show_popup", e) | ||
|
||
def get_completion_list(self, path_in): | ||
return self.completion.complete_for_folder(path_in) | ||
|
||
def completion_input(self, path_in, candidate): | ||
pattern = r"(.*[/\\:])(.*)" | ||
|
@@ -403,7 +449,9 @@ def clear(self): | |
if self.view is not None: | ||
self.view.erase_status("AdvancedNewFile") | ||
self.view.erase_status("AdvancedNewFile2") | ||
AdvancedNewFileBase.static_input_panel_view = None | ||
self.clear_input_view() | ||
self.clear_input_view_content() | ||
self.clear_input_view_project_files() | ||
|
||
def create(self, filename): | ||
base, filename = os.path.split(filename) | ||
|
@@ -455,7 +503,10 @@ def get_cursor_path(self): | |
break | ||
if (re.match(".*string.quoted.double", syntax) or | ||
re.match(".*string.quoted.single", syntax)): | ||
path = view.substr(view.extract_scope(region.begin())) | ||
point = region.begin() | ||
if (re.match(".*punctuation.definition.string.end", syntax)): | ||
point -= 1 | ||
path = view.substr(view.extract_scope(point)) | ||
path = re.sub('^"|\'', '', re.sub('"|\'$', '', path.strip())) | ||
break | ||
|
||
|
@@ -507,9 +558,6 @@ def __place_cursor_before_extension(self, view): | |
cursors.clear() | ||
cursors.add(sublime.Region(initial_position, initial_position)) | ||
|
||
def get_status_line(self): | ||
return self.view.get_status("AdvancedNewFile") | ||
|
||
def update_status_message(self, creation_path): | ||
if self.view is not None: | ||
self.view.set_status("AdvancedNewFile", creation_path) | ||
|
@@ -522,25 +570,6 @@ def get_status_prefix(self): | |
def create_status_line(self, creation_path, candidate, completion_list): | ||
return creation_path + '|' + candidate + str(completion_list) | ||
|
||
def parse_status_line(self, status_line): | ||
# Creating file at AdvancedNewFile/advanced_new_file/commands/|__init__.py['command_base.py'] | ||
if status_line: | ||
status_line = status_line.strip() | ||
index1 = status_line.rindex('[') | ||
completion_list = status_line[index1:] | ||
try: | ||
completion_list = json.loads(completion_list.replace("'", '"')) | ||
except Exception as e: | ||
print("completion_list", completion_list, e) | ||
raise e | ||
index2 = status_line.rindex('|') | ||
candidate = status_line[index2 + 1:index1] | ||
# TODO: prefix_len = len(self.get_status_prefix()) | ||
creation_path = status_line[0:index2] | ||
return (creation_path, candidate, completion_list) | ||
else: | ||
return ('', '', []) | ||
|
||
def next_candidate(self, candidate, completion_list): | ||
if candidate and completion_list: | ||
# replace the candidate with the first, and append the old candidate to the last | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sublime | ||
import sublime_plugin | ||
import os | ||
import re | ||
import xml.etree.ElementTree as ET | ||
|
||
from .new_file_command import AdvancedNewFileNew | ||
from ..lib.package_resources import get_resource | ||
from ..anf_util import * | ||
|
||
|
||
class AdvancedNewFileProjectFileCommand(AdvancedNewFileNew, sublime_plugin.WindowCommand): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this command? |
||
def __init__(self, window): | ||
super().__init__(window) | ||
|
||
def run(self, is_python=False, initial_path=None): | ||
self.is_python = is_python | ||
self.run_setup() | ||
self.show_filename_input(self.generate_initial_path(initial_path)) | ||
|
||
def get_project_folder(self): | ||
return sublime.active_window().folders()[0] | ||
|
||
def split_path(self, path_in): | ||
return (self.get_project_folder(), path_in) | ||
|
||
def entered_file_action(self, path): | ||
if self.settings.get(SHELL_INPUT_SETTING, False): | ||
self.multi_file_action(self.curly_brace_expansion(path)) | ||
else: | ||
self.single_file_action(path) | ||
|
||
def input_panel_caption(self): | ||
caption = 'Enter a path for a project file' | ||
return caption | ||
|
||
def update_status_message(self, creation_path): | ||
if self.view is not None: | ||
self.view.set_status("AdvancedNewFile", "Project file at %s" % creation_path) | ||
else: | ||
sublime.status_message("Project file at %s" % creation_path) | ||
|
||
def get_completion_list(self, path_in): | ||
return self.completion.complete_for_project(path_in) | ||
|
||
def set_input_view_project_files(self, project_files): | ||
self.get_active_view_settings().set("anf_input_view_project_files", project_files) | ||
|
||
def get_input_view_project_files(self): | ||
return self.get_active_view_settings().get("anf_input_view_project_files") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No-op. Can be removed or replaced with
super().__init__()
.