Skip to content

Commit

Permalink
Implemented java-style autoformatting. (#133)
Browse files Browse the repository at this point in the history
* Implemented java-style autoformatting.

* special case #[symbol]. This is a hack and needs to be fixed

* make sure to pass javastyle in debug as well as in reformat

* fix wording

* Made cache work properly with javastyle.
  • Loading branch information
rahularya50 authored and kavigupta committed Apr 6, 2019
1 parent 74278e2 commit ed8b20c
Show file tree
Hide file tree
Showing 8 changed files with 1,139 additions and 527 deletions.
1,605 changes: 1,094 additions & 511 deletions all_tests.scm

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion editor/format_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def __repr__(self):
def get_expression(buffer: TokenBuffer) -> Formatted:
token = buffer.pop_next_token()
comments = []
if token in SPECIALS:
if token == "#" and not buffer.done and buffer.get_next_token() == "[":
buffer.pop_next_token()
out = FormatAtom("#[" + buffer.pop_next_token().value + "]")
buffer.pop_next_token()
elif token in SPECIALS:
comments = token.comments
if token in ("(", "["):
out = get_rest_of_list(buffer, ")" if token == "(" else "]")
Expand Down
23 changes: 14 additions & 9 deletions editor/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@

CACHE_SIZE = 2 ** 8

java_newline = ""

def prettify(strings: List[str]) -> str:
def prettify(strings: List[str], javastyle: bool=False) -> str:
out = []
for i, string in enumerate(strings):
if not string.strip():
continue
out.extend(prettify_single(string))
out.extend(prettify_single(string, javastyle))

return "\n\n".join(out)


@lru_cache(CACHE_SIZE)
def prettify_single(string: str) -> List[str]:
def prettify_single(string: str, javastyle: bool) -> List[str]:
if javastyle:
global java_newline
java_newline = "\n"
out = []
buff = lexer.TokenBuffer([string], True)
while not buff.done:
Expand Down Expand Up @@ -149,7 +153,7 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]:
len(f"({operator} "))
body_str = indent("\n".join(body), INDENT // 2)
out_str = expr.open_paren + operator + " " + name_str.lstrip() + "\n" \
+ body_str + expr.close_paren
+ body_str + java_newline + expr.close_paren
return verify(make_comments(expr.comments, 0, True) + out_str, remaining)

if operator == "let":
Expand All @@ -175,7 +179,7 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]:
out_str = expr.open_paren + "let " + binding_str.lstrip() + "\n" + body_string
if expr.contents[-1].comments:
out_str += "\n"
out_str += expr.close_paren
out_str += java_newline + expr.close_paren
return verify(make_comments(expr.comments, 0, True) + out_str, remaining)

if operator == "cond":
Expand Down Expand Up @@ -220,7 +224,8 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]:
else:
# success!
out_str = make_comments(expr.comments, 0, True) + expr.open_paren + \
"cond\n" + indent("\n".join(formatted_clauses), 1) + expr.close_paren
"cond\n" + indent("\n".join(formatted_clauses), 1) + \
java_newline + expr.close_paren
out = verify(out_str, remaining)
if out[1]:
return out
Expand All @@ -240,7 +245,7 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]:
formatted_clauses.append(clause_str)

out_str = expr.open_paren + "cond\n" + indent("\n".join(formatted_clauses), 1) \
+ expr.close_paren
+ java_newline + expr.close_paren
return verify(make_comments(expr.comments, 0, True) + out_str, remaining)

# assume no special forms
Expand All @@ -256,7 +261,7 @@ def prettify_expr(expr: Formatted, remaining: int) -> Tuple[str, bool]:
out_str = expr.open_paren + operator + " " + operand_string.lstrip()
if expr.contents[-1].comments:
out_str += "\n"
out_str += expr.close_paren
out_str += java_newline + expr.close_paren
out = verify(make_comments(expr.comments, 0, True) + out_str, remaining)
return out
# but may have to go here anyway, if inlining takes up too much space
Expand Down Expand Up @@ -304,7 +309,7 @@ def prettify_data(expr: Formatted, remaining: int, is_data: bool, force_multilin
out_str = expr.open_paren + elem_string
if expr.contents and expr.contents[-1].comments:
out_str += "\n"
out_str += expr.close_paren
out_str += java_newline + expr.close_paren
return verify(make_comments(expr.comments, 0, True) + out_str, remaining)


Expand Down
7 changes: 4 additions & 3 deletions editor/local_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

state = {}

import ctypes

class Handler(server.BaseHTTPRequestHandler):
cancellation_event = threading.Event() # Shared across all instances, because the threading mixin creates a new instance every time...

Expand Down Expand Up @@ -83,10 +81,11 @@ def handle_post_thread(self, data, path):

elif path == "/reformat":
code = [x.decode("utf-8") for x in data[b"code[]"]]
javastyle = data[b"javastyle"][0] == b"true"
self.send_response(HTTPStatus.OK, 'test')
self.send_header("Content-type", "application/JSON")
self.end_headers()
self.wfile.write(bytes(json.dumps({"result": "success", "formatted": prettify(code)}), "utf-8"))
self.wfile.write(bytes(json.dumps({"result": "success", "formatted": prettify(code, javastyle)}), "utf-8"))

elif path == "/test":
self.cancellation_event.clear() # Make sure we don't have lingering cancellation requests from before
Expand Down Expand Up @@ -275,9 +274,11 @@ def supports_color():
return False
return True


class ThreadedHTTPServer(socketserver.ThreadingMixIn, server.HTTPServer):
daemon_threads = True


def start(file_args, port, open_browser):
global main_files
main_files = file_args
Expand Down
6 changes: 6 additions & 0 deletions editor/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ <h5 class="modal-title" id="settingsModalLabel">Settings</h5>
Hide frames after return. Note that frames that continue to be referenced from closures <strong> will also be hidden. </strong>
</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="javastyleCheckbox">
<label class="custom-control-label" for="javastyleCheckbox">
Enable Java-style formatting, where close parens appear on their own line, rather than being grouped with the previous line
</label>
</div>
</div>
</form>
</div>
Expand Down
2 changes: 2 additions & 0 deletions editor/static/scripts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {open} from "./layout";
import {make, request_update} from "./event_handler";
import {terminable_command} from "./canceller";
import {registerEditor, removeEditor, notify_changed} from "./test_results";
import {javastyle} from "./settings";

export {register};

Expand Down Expand Up @@ -288,6 +289,7 @@ function register(layout) {
let code = [editor.getValue()];
$.post("./reformat", {
code: code,
javastyle: javastyle(),
}).done(function (data) {
if (data) {
data = $.parseJSON(data);
Expand Down
10 changes: 8 additions & 2 deletions editor/static/scripts/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {saveState} from "./state_handler";

export {init, hide_return_frames, getAllSettings, setAllSettings};
export {init, hide_return_frames, javastyle, getAllSettings, setAllSettings};

function init() {
$("#settings-btn").click(function () {
Expand All @@ -18,12 +18,18 @@ function hide_return_frames() {
return $("#hideReturnFramesCheckbox").prop('checked');
}

function javastyle() {
return $("#javastyleCheckbox").prop('checked');
}

function getAllSettings() {
return {
"return_frames": hide_return_frames()
"return_frames": hide_return_frames(),
"javastyle": javastyle(),
}
}

function setAllSettings(data) {
$("#hideReturnFramesCheckbox").prop('checked', data["return_frames"]);
$("#javastyleCheckbox").prop('checked', data["javastyle"]);
}
7 changes: 6 additions & 1 deletion editor/static/scripts/substitution_tree_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from "./measure";
import {get_active_node} from "./navigation";

import {javastyle} from "./settings";

export {
display_tree,
get_i
Expand Down Expand Up @@ -40,7 +42,10 @@ function display_str(elem) {

async function locate(data) {
await $.post("./reformat",
{code: [display_str(data)]}).done(
{
code: [display_str(data)],
javastyle: javastyle()
}).done(
(response) => {
response = $.parseJSON(response);
data["str"] = response["formatted"];
Expand Down

0 comments on commit ed8b20c

Please sign in to comment.