-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request color presentations when clicking on a color box (#2065)
* Event unwanted * Small optimization * Add explicit check for spec violation in color box response * Pass only ColorInformation as command arguments * Use slightly more accurate color for color box * Fix underline of color box
- Loading branch information
Showing
6 changed files
with
106 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from .core.edit import parse_text_edit | ||
from .core.protocol import ColorInformation | ||
from .core.protocol import ColorPresentation | ||
from .core.protocol import ColorPresentationParams | ||
from .core.protocol import Request | ||
from .core.registry import LspTextCommand | ||
from .core.typing import List | ||
from .core.views import range_to_region | ||
from .core.views import text_document_identifier | ||
import sublime | ||
|
||
|
||
class LspColorPresentationCommand(LspTextCommand): | ||
|
||
capability = 'colorProvider' | ||
|
||
def run(self, edit: sublime.Edit, color_information: ColorInformation) -> None: | ||
session = self.best_session(self.capability) | ||
if session: | ||
self._version = self.view.change_count() | ||
self._range = color_information['range'] | ||
params = { | ||
'textDocument': text_document_identifier(self.view), | ||
'color': color_information['color'], | ||
'range': self._range | ||
} # type: ColorPresentationParams | ||
session.send_request_async(Request.colorPresentation(params, self.view), self._handle_response_async) | ||
|
||
def want_event(self) -> bool: | ||
return False | ||
|
||
def _handle_response_async(self, response: List[ColorPresentation]) -> None: | ||
if not response: | ||
return | ||
window = self.view.window() | ||
if not window: | ||
return | ||
if self._version != self.view.change_count(): | ||
return | ||
old_text = self.view.substr(range_to_region(self._range, self.view)) | ||
self._filtered_response = [] # type: List[ColorPresentation] | ||
for item in response: | ||
# Filter out items that would apply no change | ||
text_edit = item.get('textEdit') | ||
if text_edit: | ||
if text_edit['range'] == self._range and text_edit['newText'] == old_text: | ||
continue | ||
elif item['label'] == old_text: | ||
continue | ||
self._filtered_response.append(item) | ||
if self._filtered_response: | ||
window.show_quick_panel( | ||
[sublime.QuickPanelItem(item['label']) for item in self._filtered_response], | ||
self._on_select, | ||
placeholder="Change color format") | ||
|
||
def _on_select(self, index: int) -> None: | ||
if index > -1: | ||
color_pres = self._filtered_response[index] | ||
text_edit = color_pres.get('textEdit') or {'range': self._range, 'newText': color_pres['label']} | ||
self.view.run_command('lsp_apply_document_edit', {'changes': [parse_text_edit(text_edit, self._version)]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters