Próbuję napisać małą wtyczkę, aby usunąć bieżący plik i zamknąć aktywny widok. Z jakiegoś powodu self.view.file_name() zawsze zwraca None.Wysublimowany tekst: Jak uzyskać nazwę bieżącego widoku
Jestem nowy w Pythonie i nie mam pojęcia, dlaczego tak nie działa. Zgodnie z API Reference file_name() zwraca nazwę bieżącego widoku.
import sublime, sublime_plugin, send2trash
class DeleteCurrentFileCommand(sublime_plugin.TextCommand):
def run(self, edit):
f = self.view.file_name()
if (f is None):
return
send2trash.send2trash(f)
self.view.window().run_command('close')
Wyjście dir (self.view):
[ 'klasa', 'delattr', 'DICT', 'doc', "Format ',' getattribute ',' hash ',' init ',' len ' 'moduł', 'nowy', 'zmniejszyć', 'reduce_ex', 'repr', 'setattr', 'sizeof',' str”, 'subclasshook', 'weakref', 'add_regions', 'begin_edit', 'buffer_id', 'klasyfikować', 'command_history', 'em_width', 'kodowanie', 'end_edit', 'kasowania' , "erase_regions", "erase_status", "extract_completions", "extract_scope", "nazwa_pliku", "znajdź" "find_all", "find_all_results", "find_by_selector", "fold", "folded_regions", "full_line", "get_regions", "get_status", "get_symbols", "has_non_empty_selection_region", "id", "indentation_level", " indented_region ',' insert ',' is_dirty ',' is_folded ',' is_loading ',' is_read_only ',' is_scratch ',' layout_extent ',' layout_to_text ',' line ',' line_endings ',' line_height ',' lines ' "match_selector", "meta_info", "name", "replace", "retarget", "rowcol", "run_command", "nazwa zakresu", "score_selector", "sel", "set_encoding", "set_line_endings", " set_name "," set_read_only "," set_scratch "," set_status "," set_syntax_file "," set_viewport_position "," settings "," show "," show_at_center "," size "," split_by_newlines "," substr "," nazwa-syntaktyki " , 'text_point', 'text_to_layout', 'unfold', 'viewport_extent', 'viewport_position', 'visible_region', 'window', 'word']
Jakieś komunikaty o błędach w konsoli? –
Nie. Czek dla '(f jest Brak)' jest zawsze prawdziwy i dlatego zwraca zanim osiągnie 'send2trash()'. Bez sprawdzania wywołania 'send2trash()' nie działa. Po uruchomieniu 'view.file_name()' w konsoli zwraca poprawną ścieżkę. – Felix
jaki jest wynik polecenia print dir (self.view)? –