Skip to content

Commit

Permalink
format code via edit.format request
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermooo committed Jul 6, 2015
1 parent aa1f9fa commit b989e49
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 17 deletions.
26 changes: 26 additions & 0 deletions analyzer.py
Expand Up @@ -37,6 +37,8 @@
from Dart.lib.analyzer.api.protocol import CompletionGetSuggestionsParams
from Dart.lib.analyzer.api.protocol import CompletionGetSuggestionsResult
from Dart.lib.analyzer.api.protocol import CompletionResultsParams
from Dart.lib.analyzer.api.protocol import EditFormatParams
from Dart.lib.analyzer.api.protocol import EditFormatResult
from Dart.lib.analyzer.api.protocol import RemoveContentOverlay
from Dart.lib.analyzer.api.protocol import ServerGetVersionParams
from Dart.lib.analyzer.api.protocol import ServerGetVersionResult
Expand Down Expand Up @@ -434,6 +436,26 @@ def send_get_suggestions(self, view, file, offset):

self.requests.put(req, priority=TaskPriority.HIGH, block=False)

def send_format_file(self, view):
new_id = self.get_request_id()

if not view.file_name():
_logger.info("aborting sending request for formatting - no file name")
return

r0 = None
try:
r0 = view.sel()[0]
except IndexError:
r0 = sublime.Region(0)

# TODO: Implement lineLength parameter.
req = EditFormatParams(view.file_name(), r0.begin(), r0.size())
req = req.to_request(new_id)

_logger.info("now sending request for formatting")
self.requests.put(req, priority=TaskPriority.HIGH, block=False)

def should_ignore_file(self, path):
project = DartProject.from_path(path)
is_a_third_party_file = (project and is_path_under(project.path_to_packages, path))
Expand Down Expand Up @@ -507,6 +529,10 @@ def run(self):
actx.id = resp.result.id
actx.request_id = None

if isinstance(resp.result, EditFormatResult):
after(0, actions.handle_formatting, EditFormatResult.from_json(resp.result.to_json().copy()))
continue

except Exception as e:
msg = 'error in thread' + self.name + '\n'
msg += str(e)
Expand Down
20 changes: 6 additions & 14 deletions format.py
Expand Up @@ -2,15 +2,18 @@
# All rights reserved. Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.)

from subprocess import PIPE
from subprocess import Popen

import sublime
import sublime_plugin

from Dart.sublime_plugin_lib import PluginLogger
from Dart.sublime_plugin_lib.plat import supress_window

from Dart import analyzer
from Dart.lib.sdk import DartFormat

from subprocess import PIPE
from subprocess import Popen

_logger = PluginLogger(__name__)

Expand All @@ -26,18 +29,7 @@ def run(self, **kwargs):
if not view:
return

# Reformat the whole file.
text = view.substr(sublime.Region(0, view.size()))
formatted_text = DartFormat().format(text)

if not formatted_text:
sublime.status_message("Dart: Could not format anything.")
return

view.run_command('dart_replace_region', {
'region': [0, view.size()],
'text': formatted_text + '\n'
})
analyzer.g_server.send_format_file(view)


class DartReplaceRegion(sublime_plugin.TextCommand):
Expand Down
16 changes: 16 additions & 0 deletions lib/analyzer/actions.py
Expand Up @@ -195,3 +195,19 @@ def handle_completions(results):
v = get_active_view()
if v:
v.run_command('auto_complete')


def handle_formatting(result):
v = get_active_view()

v.sel().clear()

for edit in result.edits:
v.run_command('dart_replace_region', {
'region': [edit.offset, edit.length],
'text': edit.replacement
})

r = sublime.Region(result.selectionOffset,
result.selectionOffset + result.selectionLength)
v.sel().add(r)
20 changes: 17 additions & 3 deletions lib/analyzer/response.py
Expand Up @@ -13,17 +13,19 @@

from Dart._init_ import editor_context
from Dart.lib.analyzer.api.protocol import AnalysisErrorsParams
from Dart.lib.analyzer.api.protocol import ServerGetVersionResult
from Dart.lib.analyzer.api.protocol import AnalysisNavigationParams
from Dart.lib.analyzer.api.protocol import CompletionGetSuggestionsResult
from Dart.lib.analyzer.api.protocol import CompletionResultsParams
from Dart.lib.analyzer.api.protocol import ServerGetVersionResult
from Dart.lib.analyzer.api.protocol import EditFormatResult


_logger = PluginLogger(__name__)


class ResponseMaker(object):
'''Transforms raw notifications and responses into `ServerResponse`s.
'''
Transforms raw notifications and responses into `Response`s.
'''

def __init__(self, source):
Expand Down Expand Up @@ -83,8 +85,16 @@ def is_completions_suggestions_result(data):
return 'id' in data.get('result', {})


def is_format_result(data):
# TODO: find a better way to identify responses.
res = data.get('result', {})
return (res
and 'selectionOffset' in res
and 'selectionLength' in res
and 'edits' in res)


def response_classifier(data):
# XXX: replace here XXX
if is_errors_response(data):
params = AnalysisErrorsParams.from_json(data['params'])
return params.to_notification()
Expand All @@ -105,4 +115,8 @@ def response_classifier(data):
result = CompletionGetSuggestionsResult.from_json(data['result'])
return result.to_response(data['id'])

if is_format_result(data):
result = EditFormatResult.from_json(data['result'])
return result.to_response(data['id'])

return None

0 comments on commit b989e49

Please sign in to comment.