import "./cljs_env.js";
goog.provide('clojure_eval.extensions.formatting');
var module$node_modules$$codemirror$language$dist$index_cjs=shadow.js.require("module$node_modules$$codemirror$language$dist$index_cjs", {});
var module$node_modules$$codemirror$state$dist$index_cjs=shadow.js.require("module$node_modules$$codemirror$state$dist$index_cjs", {});
clojure_eval.extensions.formatting.spaces = (function clojure_eval$extensions$formatting$spaces(state,n){
return module$node_modules$$codemirror$language$dist$index_cjs.indentString(state,n);
});
clojure_eval.extensions.formatting.indent_node_props = (function clojure_eval$extensions$formatting$indent_node_props(p__55311){
var map__55312 = p__55311;
var map__55312__$1 = (((((!((map__55312 == null))))?(((((map__55312.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__55312.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__55312):map__55312);
var type = map__55312__$1;
var type_name = (function (){var obj55314 = map__55312__$1;
var k55315 = "name";
if((function (){var obj55316 = obj55314;
return (((!((obj55316 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55315,obj55316)));
})()){
return (obj55314[k55315]);
} else {
return undefined;
}
})();
return (function (p__55318){
var map__55319 = p__55318;
var map__55319__$1 = (((((!((map__55319 == null))))?(((((map__55319.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__55319.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__55319):map__55319);
var context = map__55319__$1;
var pos = (function (){var obj55325 = map__55319__$1;
var k55326 = "pos";
if((function (){var obj55328 = obj55325;
return (((!((obj55328 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55326,obj55328)));
})()){
return (obj55325[k55326]);
} else {
return undefined;
}
})();
var unit = (function (){var obj55329 = map__55319__$1;
var k55330 = "unit";
if((function (){var obj55331 = obj55329;
return (((!((obj55331 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55330,obj55331)));
})()){
return (obj55329[k55330]);
} else {
return undefined;
}
})();
var node = (function (){var obj55337 = map__55319__$1;
var k55338 = "node";
if((function (){var obj55339 = obj55337;
return (((!((obj55339 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55338,obj55339)));
})()){
return (obj55337[k55338]);
} else {
return undefined;
}
})();
var state = (function (){var obj55341 = map__55319__$1;
var k55342 = "state";
if((function (){var obj55343 = obj55341;
return (((!((obj55343 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55342,obj55343)));
})()){
return (obj55341[k55342]);
} else {
return undefined;
}
})();
if(cljs.core._EQ_.cljs$core$IFn$_invoke$arity$2("Program",type_name)){
return (0);
} else {
if(cljs.core.truth_(clojure_eval.node.coll_type_QMARK_(type))){
var G__55347 = context.column(clojure_eval.node.end(clojure_eval.node.down(node)));
if(cljs.core.truth_((function (){var and__4221__auto__ = cljs.core._EQ_.cljs$core$IFn$_invoke$arity$2("List",type_name);
if(and__4221__auto__){
var G__55352 = (function (){var G__55353 = node;
var G__55353__$1 = (((G__55353 == null))?null:clojure_eval.node.down(G__55353));
var G__55353__$2 = (((G__55353__$1 == null))?null:clojure_eval.node.right(G__55353__$1));
if((G__55353__$2 == null)){
return null;
} else {
return clojure_eval.node.name(G__55353__$2);
}
})();
var fexpr__55351 = new cljs.core.PersistentHashSet(null, new cljs.core.PersistentArrayMap(null, 3, ["DefLike",null,"NS",null,"Operator",null], null), null);
return (fexpr__55351.cljs$core$IFn$_invoke$arity$1 ? fexpr__55351.cljs$core$IFn$_invoke$arity$1(G__55352) : fexpr__55351.call(null,G__55352));
} else {
return and__4221__auto__;
}
})())){
return (G__55347 + (1));
} else {
return G__55347;
}
} else {
return (-1);

}
}
});
});
clojure_eval.extensions.formatting.props = module$node_modules$$codemirror$language$dist$index_cjs.indentNodeProp.add(clojure_eval.extensions.formatting.indent_node_props);
clojure_eval.extensions.formatting.get_indentation = (function clojure_eval$extensions$formatting$get_indentation(context,pos){
return module$node_modules$$codemirror$language$dist$index_cjs.getIndentation(context.state,pos);
});
clojure_eval.extensions.formatting.make_indent_context = (function clojure_eval$extensions$formatting$make_indent_context(state){
return (new module$node_modules$$codemirror$language$dist$index_cjs.IndentContext(state));
});
clojure_eval.extensions.formatting.indent_all = (function clojure_eval$extensions$formatting$indent_all(state){
var context = clojure_eval.extensions.formatting.make_indent_context(state);
return clojure_eval.util.update_lines(state,(function (from,content,line_num){
var current_indent = (/^\s*/.exec(content)[(0)]).length;
var indent = clojure_eval.util.guard(clojure_eval.extensions.formatting.get_indentation(context,from),cljs.core.complement(cljs.core.neg_QMARK_));
if(cljs.core.truth_(indent)){
var G__55358 = cljs.core.compare(indent,current_indent);
switch (G__55358) {
case (0):
return null;

break;
case (1):
return ({"from": (from + current_indent), "insert": clojure_eval.extensions.formatting.spaces(state,(indent - current_indent))});

break;
case (-1):
return ({"from": (from + indent), "to": (from + current_indent)});

break;
default:
throw (new Error(["No matching clause: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(G__55358)].join('')));

}
} else {
return null;
}
}));
});
clojure_eval.extensions.formatting.expected_space = (function clojure_eval$extensions$formatting$expected_space(n1,n2){
if(((clojure_eval.node.start_edge_type_QMARK_(n1)) || (((clojure_eval.node.prefix_edge_type_QMARK_(n1)) || (((clojure_eval.node.end_edge_type_QMARK_(n2)) || (clojure_eval.node.same_edge_type_QMARK_(n2)))))))){
return (0);
} else {
return (1);
}
});
clojure_eval.extensions.formatting.space_changes = (function clojure_eval$extensions$formatting$space_changes(state,from,to){
var nodes = cljs.core.reverse(cljs.core.filter.cljs$core$IFn$_invoke$arity$2((function (p1__55367_SHARP_){
return (((((from <= clojure_eval.node.start(p1__55367_SHARP_))) && ((clojure_eval.node.start(p1__55367_SHARP_) <= to)))) || ((((from <= clojure_eval.node.end(p1__55367_SHARP_))) && ((clojure_eval.node.end(p1__55367_SHARP_) <= to)))));
}),clojure_eval.node.terminal_nodes(state,from,to)));
var trim_QMARK_ = (function (){var G__55375 = cljs.core.first(nodes);
var G__55375__$1 = (((G__55375 == null))?null:clojure_eval.node.end(G__55375));
if((G__55375__$1 == null)){
return null;
} else {
return (G__55375__$1 < to);
}
})();
return cljs.core.reduce.cljs$core$IFn$_invoke$arity$3((function (out,p__55376){
var vec__55378 = p__55376;
var map__55382 = cljs.core.nth.cljs$core$IFn$_invoke$arity$3(vec__55378,(0),null);
var map__55382__$1 = (((((!((map__55382 == null))))?(((((map__55382.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__55382.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__55382):map__55382);
var n2 = (function (){var obj55390 = map__55382__$1;
var k55391 = "type";
if((function (){var obj55393 = obj55390;
return (((!((obj55393 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55391,obj55393)));
})()){
return (obj55390[k55391]);
} else {
return undefined;
}
})();
var start2 = (function (){var obj55398 = map__55382__$1;
var k55399 = "from";
if((function (){var obj55400 = obj55398;
return (((!((obj55400 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55399,obj55400)));
})()){
return (obj55398[k55399]);
} else {
return undefined;
}
})();
var end2 = (function (){var obj55404 = map__55382__$1;
var k55405 = "to";
if((function (){var obj55406 = obj55404;
return (((!((obj55406 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55405,obj55406)));
})()){
return (obj55404[k55405]);
} else {
return undefined;
}
})();
var map__55383 = cljs.core.nth.cljs$core$IFn$_invoke$arity$3(vec__55378,(1),null);
var map__55383__$1 = (((((!((map__55383 == null))))?(((((map__55383.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__55383.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__55383):map__55383);
var n1 = (function (){var obj55412 = map__55383__$1;
var k55413 = "type";
if((function (){var obj55414 = obj55412;
return (((!((obj55414 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55413,obj55414)));
})()){
return (obj55412[k55413]);
} else {
return undefined;
}
})();
var start1 = (function (){var obj55416 = map__55383__$1;
var k55417 = "from";
if((function (){var obj55418 = obj55416;
return (((!((obj55418 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55417,obj55418)));
})()){
return (obj55416[k55417]);
} else {
return undefined;
}
})();
var end1 = (function (){var obj55419 = map__55383__$1;
var k55420 = "to";
if((function (){var obj55421 = obj55419;
return (((!((obj55421 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55420,obj55421)));
})()){
return (obj55419[k55420]);
} else {
return undefined;
}
})();
var expected = clojure_eval.extensions.formatting.expected_space(n1,n2);
var actual = (start2 - end1);
var G__55422 = cljs.core.compare(actual,expected);
switch (G__55422) {
case (0):
return out;

break;
case (1):
var array55423 = out;
array55423.push(({"from": (((expected === (0)))?end1:(end1 + (1))), "to": start2}));

return array55423;

break;
case (-1):
var array55424 = out;
array55424.push(({"from": end1, "insert": " "}));

return array55424;

break;
default:
return out;

}
}),(cljs.core.truth_(trim_QMARK_)?[({"from":clojure_eval.node.end(cljs.core.first(nodes)),"to":to})]:[]),cljs.core.partition.cljs$core$IFn$_invoke$arity$3((2),(1),nodes));
});
clojure_eval.extensions.formatting.into_arr = (function clojure_eval$extensions$formatting$into_arr(arr,items){
var seq__55427_55501 = cljs.core.seq(items);
var chunk__55428_55502 = null;
var count__55429_55503 = (0);
var i__55430_55504 = (0);
while(true){
if((i__55430_55504 < count__55429_55503)){
var i_55507 = chunk__55428_55502.cljs$core$IIndexed$_nth$arity$2(null,i__55430_55504);
arr.push(i_55507);


var G__55509 = seq__55427_55501;
var G__55510 = chunk__55428_55502;
var G__55511 = count__55429_55503;
var G__55512 = (i__55430_55504 + (1));
seq__55427_55501 = G__55509;
chunk__55428_55502 = G__55510;
count__55429_55503 = G__55511;
i__55430_55504 = G__55512;
continue;
} else {
var temp__5753__auto___55513 = cljs.core.seq(seq__55427_55501);
if(temp__5753__auto___55513){
var seq__55427_55514__$1 = temp__5753__auto___55513;
if(cljs.core.chunked_seq_QMARK_(seq__55427_55514__$1)){
var c__4649__auto___55515 = cljs.core.chunk_first(seq__55427_55514__$1);
var G__55516 = cljs.core.chunk_rest(seq__55427_55514__$1);
var G__55517 = c__4649__auto___55515;
var G__55518 = cljs.core.count(c__4649__auto___55515);
var G__55519 = (0);
seq__55427_55501 = G__55516;
chunk__55428_55502 = G__55517;
count__55429_55503 = G__55518;
i__55430_55504 = G__55519;
continue;
} else {
var i_55520 = cljs.core.first(seq__55427_55514__$1);
arr.push(i_55520);


var G__55521 = cljs.core.next(seq__55427_55514__$1);
var G__55522 = null;
var G__55523 = (0);
var G__55524 = (0);
seq__55427_55501 = G__55521;
chunk__55428_55502 = G__55522;
count__55429_55503 = G__55523;
i__55430_55504 = G__55524;
continue;
}
} else {
}
}
break;
}

return arr;
});
/**
 * Returns mutated `changes` array
 */
clojure_eval.extensions.formatting.format_line = (function clojure_eval$extensions$formatting$format_line(state,indent_context,from,text,line_num,changes,format_spaces_QMARK_){
if((!((text == null)))){
} else {
throw (new Error("Assert failed: (some? text)"));
}

var current_indent = (/^\s*/.exec(text)[(0)]).length;
var indent = clojure_eval.util.guard(clojure_eval.extensions.formatting.get_indentation(indent_context,from),cljs.core.complement(cljs.core.neg_QMARK_));
var indentation_change = (cljs.core.truth_(indent)?(function (){var G__55431 = cljs.core.compare(indent,current_indent);
switch (G__55431) {
case (0):
return null;

break;
case (1):
return ({"from": (from + current_indent), "insert": clojure_eval.extensions.formatting.spaces(state,(indent - current_indent))});

break;
case (-1):
return ({"from": (from + indent), "to": (from + current_indent)});

break;
default:
throw (new Error(["No matching clause: ",cljs.core.str.cljs$core$IFn$_invoke$arity$1(G__55431)].join('')));

}
})():null);
var space_changes = (cljs.core.truth_((function (){var and__4221__auto__ = format_spaces_QMARK_;
if(cljs.core.truth_(and__4221__auto__)){
return clojure_eval.node.within_program_QMARK_.cljs$core$IFn$_invoke$arity$2(state,from);
} else {
return and__4221__auto__;
}
})())?clojure_eval.extensions.formatting.space_changes(state,(from + current_indent),(from + cljs.core.count(text))):null);
var G__55432 = changes;
var G__55432__$1 = (cljs.core.truth_(space_changes)?clojure_eval.extensions.formatting.into_arr(G__55432,space_changes):G__55432);
if(cljs.core.truth_(indentation_change)){
var array55433 = G__55432__$1;
array55433.push(indentation_change);

return array55433;
} else {
return G__55432__$1;
}
});
clojure_eval.extensions.formatting.format_selection = (function clojure_eval$extensions$formatting$format_selection(state){
var context = clojure_eval.extensions.formatting.make_indent_context(state);
return clojure_eval.util.update_selected_lines(state,(function (p__55434,changes,range){
var map__55435 = p__55434;
var map__55435__$1 = (((((!((map__55435 == null))))?(((((map__55435.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__55435.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__55435):map__55435);
var line = map__55435__$1;
var from = (function (){var obj55437 = map__55435__$1;
var k55438 = "from";
if((function (){var obj55439 = obj55437;
return (((!((obj55439 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55438,obj55439)));
})()){
return (obj55437[k55438]);
} else {
return undefined;
}
})();
var text = (function (){var obj55440 = map__55435__$1;
var k55441 = "text";
if((function (){var obj55442 = obj55440;
return (((!((obj55442 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55441,obj55442)));
})()){
return (obj55440[k55441]);
} else {
return undefined;
}
})();
var number = (function (){var obj55443 = map__55435__$1;
var k55444 = "number";
if((function (){var obj55445 = obj55443;
return (((!((obj55445 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k55444,obj55445)));
})()){
return (obj55443[k55444]);
} else {
return undefined;
}
})();
return clojure_eval.extensions.formatting.format_line(state,context,from,text,number,changes,true);
}));
});
clojure_eval.extensions.formatting.format_all = (function clojure_eval$extensions$formatting$format_all(state){
var context = clojure_eval.extensions.formatting.make_indent_context(state);
return clojure_eval.util.update_lines(state,(function (from,text,line_num){
return clojure_eval.extensions.formatting.format_line(state,context,from,text,line_num,[],true);
}));
});
clojure_eval.extensions.formatting.format_transaction = (function clojure_eval$extensions$formatting$format_transaction(tr){
var origin = clojure_eval.util.get_user_event_annotation(tr);
var temp__5755__auto__ = (cljs.core.truth_(clojure_eval.node.within_program_QMARK_.cljs$core$IFn$_invoke$arity$1(tr.startState))?(function (){var G__55446 = origin;
switch (G__55446) {
case "input":
case "input.type":
case "delete":
case "keyboardselection":
case "pointerselection":
case "select.pointer":
case "cut":
case "noformat":
case "evalregion":
return null;

break;
case "format-selections":
return clojure_eval.extensions.formatting.format_selection(tr.state);

break;
default:
if(cljs.core.truth_(tr.changes.empty)){
return null;
} else {
var state = tr.state;
var context = clojure_eval.extensions.formatting.make_indent_context(state);
return clojure_eval.util.iter_changed_lines(tr,(function (line,changes){
return clojure_eval.extensions.formatting.format_line(state,context,line.from,line.text,line.number,changes,true);
}));
}

}
})():null);
if((temp__5755__auto__ == null)){
return tr;
} else {
var changes = temp__5755__auto__;
return tr.startState.update((function (){var obj55449 = changes;
var obj55450 = (((!((obj55449 == null))))?obj55449:({}));
(obj55450["filter"] = false);

return obj55450;
})());
}
});
clojure_eval.extensions.formatting.format = (function clojure_eval$extensions$formatting$format(state){
if(clojure_eval.util.something_selected_QMARK_(state)){
return state.update(clojure_eval.extensions.formatting.format_selection(state));
} else {
return clojure_eval.extensions.formatting.format_all(state);
}
});
clojure_eval.extensions.formatting.prefix_all = (function clojure_eval$extensions$formatting$prefix_all(prefix,state){
return clojure_eval.util.update_lines(state,(function (from,_,___$1){
return ({"from": from, "insert": prefix});
}));
});
clojure_eval.extensions.formatting.ext_format_changed_lines = (function clojure_eval$extensions$formatting$ext_format_changed_lines(){
return module$node_modules$$codemirror$state$dist$index_cjs.EditorState.transactionFilter.of(clojure_eval.extensions.formatting.format_transaction);
});

//# sourceMappingURL=clojure_eval.extensions.formatting.js.map
