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

Add format on paste #2397

Merged
merged 22 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
18f4f21
add format on paste
predragnikolic Jan 22, 2024
07f23a2
include whitespace to improve format on paste
predragnikolic Jan 22, 2024
54c1b64
tweak comment
predragnikolic Jan 22, 2024
0c6068b
fix flake
predragnikolic Jan 22, 2024
0db49fe
move before run_command
predragnikolic Jan 24, 2024
3a2190c
move up to be before the early return
predragnikolic Jan 24, 2024
017eac7
introduce lsp_format_on_paste instead of format_on_paste
predragnikolic Jan 24, 2024
08f53ea
fix flake
predragnikolic Jan 24, 2024
fdfdbaf
fix over indent flake :(
predragnikolic Jan 24, 2024
8bf0685
use $ref for lsp_format_on_paste
predragnikolic Jan 25, 2024
c9fe44f
Rafal having good suggestions as always :)
predragnikolic Jan 25, 2024
c9d1b85
handle paste_and_indent as well if lsp_format_on_paste is enabled
predragnikolic Jan 25, 2024
f654bc8
make sure that we have LSP running before we override the paste_and_i…
predragnikolic Jan 25, 2024
162527c
Merge branch 'main' into feat/format-on-paste-2
predragnikolic Jan 26, 2024
6850d20
Merge branch 'main' into feat/format-on-paste-2
predragnikolic Jan 27, 2024
4346bae
Update plugin/documents.py
predragnikolic Jan 29, 2024
7746215
fix :
predragnikolic Jan 29, 2024
3d52e7a
the first paragraph is not necessary
predragnikolic Jan 29, 2024
861d047
remove unnecessary line and move inside if
predragnikolic Jan 29, 2024
0cfcc71
avoid quick flash of selected region before format is run
predragnikolic Feb 1, 2024
1a50fb9
update text to be more user friendly
predragnikolic Feb 1, 2024
d8f60eb
Merge branch 'main' into feat/format-on-paste-2
predragnikolic Feb 2, 2024
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
3 changes: 3 additions & 0 deletions LSP.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

// --- Document Formatting ------------------------------------------------------------

// Run the server's documentRangeFormattingProvider (if supported) on the pasted text.
"format_on_paste": false,

// Run the server's formatProvider (if supported) on a file before saving.
// This option is also supported in syntax-specific settings and/or in the
// "settings" section of project files.
Expand Down
2 changes: 2 additions & 0 deletions plugin/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class Settings:
diagnostics_panel_include_severity_level = cast(int, None)
disabled_capabilities = cast(List[str], None)
document_highlight_style = cast(str, None)
format_on_paste = cast(bool, None)
hover_highlight_style = cast(str, None)
inhibit_snippet_completions = cast(bool, None)
inhibit_word_completions = cast(bool, None)
Expand Down Expand Up @@ -250,6 +251,7 @@ def r(name: str, default: Union[bool, int, str, list, dict]) -> None:
r("diagnostics_panel_include_severity_level", 4)
r("disabled_capabilities", [])
r("document_highlight_style", "underline")
r("format_on_paste", False)
r("hover_highlight_style", "")
r("initially_folded", [])
r("link_highlight_style", "underline")
Expand Down
34 changes: 33 additions & 1 deletion plugin/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def on_change() -> None:
self._registration = SettingsRegistration(view.settings(), on_change=on_change)
self._completions_task = None # type: Optional[QueryCompletionsTask]
self._stored_selection = [] # type: List[sublime.Region]
self._did_paste = False
self._setup()

def __del__(self) -> None:
Expand Down Expand Up @@ -528,7 +529,9 @@ def on_text_command(self, command_name: str, args: Optional[dict]) -> Optional[T
return None

def on_post_text_command(self, command_name: str, args: Optional[Dict[str, Any]]) -> None:
if command_name in ("next_field", "prev_field") and args is None:
if command_name == 'paste' and userprefs().format_on_paste:
self._did_paste = True
elif command_name in ("next_field", "prev_field") and args is None:
sublime.set_timeout_async(lambda: self.do_signature_help_async(manual=True))
if not self.view.is_popup_visible():
return
Expand Down Expand Up @@ -936,6 +939,9 @@ def _on_view_updated_async(self) -> None:
self._when_selection_remains_stable_async(
self._do_highlights_async, first_region, after_ms=self.highlights_debounce_time)
self.do_signature_help_async(manual=False)
if self._did_paste:
self._did_paste = False
self._format_on_paste_async()

def _update_stored_selection_async(self) -> Tuple[Optional[sublime.Region], bool]:
"""
Expand All @@ -959,6 +965,32 @@ def _update_stored_selection_async(self) -> Tuple[Optional[sublime.Region], bool
self._stored_selection = selection
return changed_first_region, True

def _format_on_paste_async(self) -> None:
self.purge_changes_async()
clipboard_text = sublime.get_clipboard()
sel = self.view.sel()
split_clipboard_text = clipboard_text.split('\n')
multi_cursor_paste = len(split_clipboard_text) == len(sel) and len(sel) > 1
original_selection = list(sel)
regions_to_format = [] # type: List[sublime.Region]
pasted_text = clipboard_text
# add regions to selection, in order for lsp_format_document_range to format those regions
for index, region in enumerate(sel):
if multi_cursor_paste:
pasted_text = split_clipboard_text[index]
pasted_region = self.view.find(pasted_text, region.end(), sublime.REVERSE | sublime.LITERAL)
if pasted_region:
# Including whitespace may help servers format a range better
# More info at https://github.com/sublimelsp/LSP/pull/2311#issuecomment-1688593038
a = self.view.find_by_class(pasted_region.a, False,
sublime.CLASS_WORD_END | sublime.CLASS_PUNCTUATION_END)
formatting_region = sublime.Region(a, pasted_region.b)
regions_to_format.append(formatting_region)
sel.add_all(regions_to_format)
self.view.run_command('lsp_format_document_range')
sel.clear()
sel.add_all(original_selection)

def _clear_session_views_async(self) -> None:
session_views = self._session_views

Expand Down
5 changes: 5 additions & 0 deletions sublime-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,11 @@
"default": false,
"markdownDescription": "Show errors and warnings count in the status bar."
},
"format_on_paste": {
"type": "boolean",
"default": false,
"markdownDescription": "Run the server's documentRangeFormattingProvider (if supported) on the pasted text."
},
"lsp_format_on_save": {
"$ref": "sublime://settings/LSP#/definitions/lsp_format_on_save"
},
Expand Down