import "./cljs_env.js";
goog.provide('nextjournal.clojure_mode.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", {});
nextjournal.clojure_mode.extensions.formatting.spaces = (function nextjournal$clojure_mode$extensions$formatting$spaces(state,n){
return module$node_modules$$codemirror$language$dist$index_cjs.indentString(state,n);
});
nextjournal.clojure_mode.extensions.formatting.indent_node_props = (function nextjournal$clojure_mode$extensions$formatting$indent_node_props(p__58226){
var map__58233 = p__58226;
var map__58233__$1 = (((((!((map__58233 == null))))?(((((map__58233.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__58233.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__58233):map__58233);
var type = map__58233__$1;
var type_name = (function (){var obj58240 = map__58233__$1;
var k58241 = "name";
if((function (){var obj58243 = obj58240;
return (((!((obj58243 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58241,obj58243)));
})()){
return (obj58240[k58241]);
} else {
return undefined;
}
})();
return (function (p__58249){
var map__58252 = p__58249;
var map__58252__$1 = (((((!((map__58252 == null))))?(((((map__58252.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__58252.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__58252):map__58252);
var context = map__58252__$1;
var pos = (function (){var obj58258 = map__58252__$1;
var k58259 = "pos";
if((function (){var obj58260 = obj58258;
return (((!((obj58260 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58259,obj58260)));
})()){
return (obj58258[k58259]);
} else {
return undefined;
}
})();
var unit = (function (){var obj58262 = map__58252__$1;
var k58263 = "unit";
if((function (){var obj58264 = obj58262;
return (((!((obj58264 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58263,obj58264)));
})()){
return (obj58262[k58263]);
} else {
return undefined;
}
})();
var node = (function (){var obj58268 = map__58252__$1;
var k58269 = "node";
if((function (){var obj58270 = obj58268;
return (((!((obj58270 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58269,obj58270)));
})()){
return (obj58268[k58269]);
} else {
return undefined;
}
})();
var state = (function (){var obj58273 = map__58252__$1;
var k58274 = "state";
if((function (){var obj58275 = obj58273;
return (((!((obj58275 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58274,obj58275)));
})()){
return (obj58273[k58274]);
} else {
return undefined;
}
})();
if(cljs.core._EQ_.cljs$core$IFn$_invoke$arity$2("Program",type_name)){
return (0);
} else {
if(cljs.core.truth_(nextjournal.clojure_mode.node.coll_type_QMARK_(type))){
var G__58280 = context.column(nextjournal.clojure_mode.node.end(nextjournal.clojure_mode.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__58282 = (function (){var G__58283 = node;
var G__58283__$1 = (((G__58283 == null))?null:nextjournal.clojure_mode.node.down(G__58283));
var G__58283__$2 = (((G__58283__$1 == null))?null:nextjournal.clojure_mode.node.right(G__58283__$1));
if((G__58283__$2 == null)){
return null;
} else {
return nextjournal.clojure_mode.node.name(G__58283__$2);
}
})();
var fexpr__58281 = new cljs.core.PersistentHashSet(null, new cljs.core.PersistentArrayMap(null, 3, ["DefLike",null,"NS",null,"Operator",null], null), null);
return (fexpr__58281.cljs$core$IFn$_invoke$arity$1 ? fexpr__58281.cljs$core$IFn$_invoke$arity$1(G__58282) : fexpr__58281.call(null,G__58282));
} else {
return and__4221__auto__;
}
})())){
return (G__58280 + (1));
} else {
return G__58280;
}
} else {
return (-1);

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

break;
case (1):
return ({"from": (from + current_indent), "insert": nextjournal.clojure_mode.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__58305)].join('')));

}
} else {
return null;
}
}));
});
nextjournal.clojure_mode.extensions.formatting.expected_space = (function nextjournal$clojure_mode$extensions$formatting$expected_space(n1,n2){
if(((nextjournal.clojure_mode.node.start_edge_type_QMARK_(n1)) || (((nextjournal.clojure_mode.node.prefix_edge_type_QMARK_(n1)) || (((nextjournal.clojure_mode.node.end_edge_type_QMARK_(n2)) || (nextjournal.clojure_mode.node.same_edge_type_QMARK_(n2)))))))){
return (0);
} else {
return (1);
}
});
nextjournal.clojure_mode.extensions.formatting.space_changes = (function nextjournal$clojure_mode$extensions$formatting$space_changes(state,from,to){
var nodes = cljs.core.reverse(cljs.core.filter.cljs$core$IFn$_invoke$arity$2((function (p1__58319_SHARP_){
return (((((from <= nextjournal.clojure_mode.node.start(p1__58319_SHARP_))) && ((nextjournal.clojure_mode.node.start(p1__58319_SHARP_) <= to)))) || ((((from <= nextjournal.clojure_mode.node.end(p1__58319_SHARP_))) && ((nextjournal.clojure_mode.node.end(p1__58319_SHARP_) <= to)))));
}),nextjournal.clojure_mode.node.terminal_nodes(state,from,to)));
var trim_QMARK_ = (function (){var G__58331 = cljs.core.first(nodes);
var G__58331__$1 = (((G__58331 == null))?null:nextjournal.clojure_mode.node.end(G__58331));
if((G__58331__$1 == null)){
return null;
} else {
return (G__58331__$1 < to);
}
})();
return cljs.core.reduce.cljs$core$IFn$_invoke$arity$3((function (out,p__58338){
var vec__58341 = p__58338;
var map__58345 = cljs.core.nth.cljs$core$IFn$_invoke$arity$3(vec__58341,(0),null);
var map__58345__$1 = (((((!((map__58345 == null))))?(((((map__58345.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__58345.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__58345):map__58345);
var n2 = (function (){var obj58351 = map__58345__$1;
var k58352 = "type";
if((function (){var obj58353 = obj58351;
return (((!((obj58353 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58352,obj58353)));
})()){
return (obj58351[k58352]);
} else {
return undefined;
}
})();
var start2 = (function (){var obj58354 = map__58345__$1;
var k58355 = "from";
if((function (){var obj58356 = obj58354;
return (((!((obj58356 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58355,obj58356)));
})()){
return (obj58354[k58355]);
} else {
return undefined;
}
})();
var end2 = (function (){var obj58357 = map__58345__$1;
var k58358 = "to";
if((function (){var obj58359 = obj58357;
return (((!((obj58359 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58358,obj58359)));
})()){
return (obj58357[k58358]);
} else {
return undefined;
}
})();
var map__58346 = cljs.core.nth.cljs$core$IFn$_invoke$arity$3(vec__58341,(1),null);
var map__58346__$1 = (((((!((map__58346 == null))))?(((((map__58346.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__58346.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__58346):map__58346);
var n1 = (function (){var obj58364 = map__58346__$1;
var k58365 = "type";
if((function (){var obj58367 = obj58364;
return (((!((obj58367 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58365,obj58367)));
})()){
return (obj58364[k58365]);
} else {
return undefined;
}
})();
var start1 = (function (){var obj58368 = map__58346__$1;
var k58369 = "from";
if((function (){var obj58372 = obj58368;
return (((!((obj58372 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58369,obj58372)));
})()){
return (obj58368[k58369]);
} else {
return undefined;
}
})();
var end1 = (function (){var obj58374 = map__58346__$1;
var k58375 = "to";
if((function (){var obj58376 = obj58374;
return (((!((obj58376 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58375,obj58376)));
})()){
return (obj58374[k58375]);
} else {
return undefined;
}
})();
var expected = nextjournal.clojure_mode.extensions.formatting.expected_space(n1,n2);
var actual = (start2 - end1);
var G__58378 = cljs.core.compare(actual,expected);
switch (G__58378) {
case (0):
return out;

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

return array58379;

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

return array58381;

break;
default:
return out;

}
}),(cljs.core.truth_(trim_QMARK_)?[({"from":nextjournal.clojure_mode.node.end(cljs.core.first(nodes)),"to":to})]:[]),cljs.core.partition.cljs$core$IFn$_invoke$arity$3((2),(1),nodes));
});
nextjournal.clojure_mode.extensions.formatting.into_arr = (function nextjournal$clojure_mode$extensions$formatting$into_arr(arr,items){
var seq__58386_58478 = cljs.core.seq(items);
var chunk__58387_58479 = null;
var count__58388_58480 = (0);
var i__58389_58481 = (0);
while(true){
if((i__58389_58481 < count__58388_58480)){
var i_58483 = chunk__58387_58479.cljs$core$IIndexed$_nth$arity$2(null,i__58389_58481);
arr.push(i_58483);


var G__58484 = seq__58386_58478;
var G__58485 = chunk__58387_58479;
var G__58486 = count__58388_58480;
var G__58487 = (i__58389_58481 + (1));
seq__58386_58478 = G__58484;
chunk__58387_58479 = G__58485;
count__58388_58480 = G__58486;
i__58389_58481 = G__58487;
continue;
} else {
var temp__5753__auto___58488 = cljs.core.seq(seq__58386_58478);
if(temp__5753__auto___58488){
var seq__58386_58491__$1 = temp__5753__auto___58488;
if(cljs.core.chunked_seq_QMARK_(seq__58386_58491__$1)){
var c__4649__auto___58492 = cljs.core.chunk_first(seq__58386_58491__$1);
var G__58493 = cljs.core.chunk_rest(seq__58386_58491__$1);
var G__58494 = c__4649__auto___58492;
var G__58495 = cljs.core.count(c__4649__auto___58492);
var G__58496 = (0);
seq__58386_58478 = G__58493;
chunk__58387_58479 = G__58494;
count__58388_58480 = G__58495;
i__58389_58481 = G__58496;
continue;
} else {
var i_58498 = cljs.core.first(seq__58386_58491__$1);
arr.push(i_58498);


var G__58499 = cljs.core.next(seq__58386_58491__$1);
var G__58500 = null;
var G__58501 = (0);
var G__58502 = (0);
seq__58386_58478 = G__58499;
chunk__58387_58479 = G__58500;
count__58388_58480 = G__58501;
i__58389_58481 = G__58502;
continue;
}
} else {
}
}
break;
}

return arr;
});
/**
 * Returns mutated `changes` array
 */
nextjournal.clojure_mode.extensions.formatting.format_line = (function nextjournal$clojure_mode$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 = nextjournal.clojure_mode.util.guard(nextjournal.clojure_mode.extensions.formatting.get_indentation(indent_context,from),cljs.core.complement(cljs.core.neg_QMARK_));
var indentation_change = (cljs.core.truth_(indent)?(function (){var G__58401 = cljs.core.compare(indent,current_indent);
switch (G__58401) {
case (0):
return null;

break;
case (1):
return ({"from": (from + current_indent), "insert": nextjournal.clojure_mode.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__58401)].join('')));

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

return array58407;
} else {
return G__58405__$1;
}
});
nextjournal.clojure_mode.extensions.formatting.format_selection = (function nextjournal$clojure_mode$extensions$formatting$format_selection(state){
var context = nextjournal.clojure_mode.extensions.formatting.make_indent_context(state);
return nextjournal.clojure_mode.util.update_selected_lines(state,(function (p__58414,changes,range){
var map__58418 = p__58414;
var map__58418__$1 = (((((!((map__58418 == null))))?(((((map__58418.cljs$lang$protocol_mask$partition0$ & (64))) || ((cljs.core.PROTOCOL_SENTINEL === map__58418.cljs$core$ISeq$))))?true:false):false))?cljs.core.apply.cljs$core$IFn$_invoke$arity$2(cljs.core.hash_map,map__58418):map__58418);
var line = map__58418__$1;
var from = (function (){var obj58423 = map__58418__$1;
var k58424 = "from";
if((function (){var obj58427 = obj58423;
return (((!((obj58427 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58424,obj58427)));
})()){
return (obj58423[k58424]);
} else {
return undefined;
}
})();
var text = (function (){var obj58430 = map__58418__$1;
var k58431 = "text";
if((function (){var obj58433 = obj58430;
return (((!((obj58433 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58431,obj58433)));
})()){
return (obj58430[k58431]);
} else {
return undefined;
}
})();
var number = (function (){var obj58434 = map__58418__$1;
var k58435 = "number";
if((function (){var obj58438 = obj58434;
return (((!((obj58438 == null)))) && (applied_science.js_interop.impl.in_QMARK__STAR_(k58435,obj58438)));
})()){
return (obj58434[k58435]);
} else {
return undefined;
}
})();
return nextjournal.clojure_mode.extensions.formatting.format_line(state,context,from,text,number,changes,true);
}));
});
nextjournal.clojure_mode.extensions.formatting.format_all = (function nextjournal$clojure_mode$extensions$formatting$format_all(state){
var context = nextjournal.clojure_mode.extensions.formatting.make_indent_context(state);
return nextjournal.clojure_mode.util.update_lines(state,(function (from,text,line_num){
return nextjournal.clojure_mode.extensions.formatting.format_line(state,context,from,text,line_num,[],true);
}));
});
nextjournal.clojure_mode.extensions.formatting.format_transaction = (function nextjournal$clojure_mode$extensions$formatting$format_transaction(tr){
var origin = nextjournal.clojure_mode.util.get_user_event_annotation(tr);
var temp__5755__auto__ = (cljs.core.truth_(nextjournal.clojure_mode.node.within_program_QMARK_.cljs$core$IFn$_invoke$arity$1(tr.startState))?(function (){var G__58441 = origin;
switch (G__58441) {
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 nextjournal.clojure_mode.extensions.formatting.format_selection(tr.state);

break;
default:
if(cljs.core.truth_(tr.changes.empty)){
return null;
} else {
var state = tr.state;
var context = nextjournal.clojure_mode.extensions.formatting.make_indent_context(state);
return nextjournal.clojure_mode.util.iter_changed_lines(tr,(function (line,changes){
return nextjournal.clojure_mode.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 obj58445 = changes;
var obj58446 = (((!((obj58445 == null))))?obj58445:({}));
(obj58446["filter"] = false);

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

//# sourceMappingURL=nextjournal.clojure_mode.extensions.formatting.js.map
