Skip to content
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

Adds a lsp_save_all function #1876

Merged
merged 7 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
}
]
},
// Save all open files with lsp_save
// {
// "command": "lsp_save_all",
// "keys": [
// "UNBOUND"
// ]
// },
// Run Code Action
// {
// "command": "lsp_code_actions",
Expand Down
1 change: 1 addition & 0 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from .plugin.panels import LspToggleServerPanelCommand
from .plugin.references import LspSymbolReferencesCommand
from .plugin.rename import LspSymbolRenameCommand
from .plugin.save_command import LspSaveAllCommand
from .plugin.save_command import LspSaveCommand
from .plugin.selection_range import LspExpandSelectionCommand
from .plugin.symbols import LspDocumentSymbolsCommand
Expand Down
10 changes: 10 additions & 0 deletions plugin/core/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def open_file(
window: sublime.Window, file_path: str, flags: int = 0, group: int = -1
) -> Promise[Optional[sublime.View]]:
"""Open a file asynchronously. It is only safe to call this function from the UI thread."""

# window.open_file brings the file to focus if it's already opened, which we don't want.
# So we first check if there's already a view for that file.
view = window.find_open_file(file_path)
if view:
return Promise.resolve(view)

view = window.open_file(file_path, flags, group)
if not view.is_loading():
# It's already loaded. Possibly already open in a tab.
Expand Down Expand Up @@ -46,6 +53,9 @@ def fullfill(resolve: ResolveFunc[Optional[sublime.View]]) -> None:
def center_selection(v: sublime.View, r: RangeLsp) -> sublime.View:
selection = range_to_region(Range.from_lsp(r), v)
v.run_command("lsp_selection_set", {"regions": [(selection.a, selection.a)]})
window = v.window()
if window:
window.focus_view(v)
v.show_at_center(selection)
return v

Expand Down
13 changes: 13 additions & 0 deletions plugin/save_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,16 @@ def _on_task_completed_async(self) -> None:
def _trigger_native_save(self) -> None:
# Triggered from set_timeout to preserve original semantics of on_pre_save handling
sublime.set_timeout(lambda: self.view.run_command('save', {"async": True}))


class LspSaveAllCommand(sublime_plugin.WindowCommand):
def run(self) -> None:
done = set()
for view in self.window.views():
buffer_id = view.buffer_id()
if buffer_id in done:
continue
if not view.is_dirty():
continue
done.add(buffer_id)
view.run_command("lsp_save", None)