MCPcopy
hub / github.com/witheve/Eve / formatSpan

Method formatSpan

src/ide.ts:1215–1263  ·  view source on GitHub ↗

Create a new span representing the given source, collapsing and splitting existing spans as required to maintain invariants.

(from:Position, to:Position, source:any)

Source from the content-addressed store, hash-verified

1213
1214 /** Create a new span representing the given source, collapsing and splitting existing spans as required to maintain invariants. */
1215 formatSpan(from:Position, to:Position, source:any):Span[] {
1216 let selection = {from, to};
1217
1218 let spans = this.findSpans(from, to, source.type);
1219 let formatted = false;
1220 let neue:Span[] = [];
1221 for(let span of spans) {
1222 let loc = span.find();
1223 if(!loc) continue;
1224
1225 // If the formatted range matches an existing span of the same type, clear it.
1226 if(samePosition(loc.from, from) && samePosition(loc.to, to)) {
1227 span.clear();
1228 formatted = true;
1229
1230 // If formatted range wholly encloses a span of the same type, clear it.
1231 } else if(whollyEnclosed(loc, selection)) {
1232 span.clear();
1233
1234 // If the formatted range is wholly enclosed in a span of the same type, split the span around it.
1235 } else if(whollyEnclosed(selection, loc)) {
1236 if(!samePosition(loc.from, from)) neue.push(this.markSpan(loc.from, from, source));
1237 if(!samePosition(to, loc.to)) neue.push(this.markSpan(to, loc.to, source));
1238 span.clear();
1239 formatted = true;
1240
1241 // If the formatted range intersects the end of a span of the same type, clear the intersection.
1242 } else if(comparePositions(loc.to, from) > 0) {
1243 neue.push(this.markSpan(loc.from, from, source));
1244 span.clear();
1245
1246 // If the formatted range intersects the start of a span of the same type, clear the intersection.
1247 } else if(comparePositions(loc.from, to) < 0) {
1248 neue.push(this.markSpan(to, loc.to, source));
1249 span.clear();
1250 }
1251 }
1252
1253 // If we haven't already formatted by removing existing span(s) then we should create a new span
1254 if(!formatted) {
1255 neue.push(this.markSpan(from, to, source));
1256 }
1257
1258 for(let span of neue) {
1259 this.trackDenormalized(span);
1260 }
1261
1262 return neue;
1263 }
1264
1265 format(source:{type:string, info?:string, level?: number, listData?: {type:"ordered"|"bullet", start?: number}}, refocus = false) {
1266 let SpanClass:(typeof Span) = spanTypes[source.type] || spanTypes["default"];

Callers 3

formatInlineMethod · 0.95
formatLineMethod · 0.95
formatBlockMethod · 0.95

Calls 8

findSpansMethod · 0.95
markSpanMethod · 0.95
trackDenormalizedMethod · 0.95
samePositionFunction · 0.90
whollyEnclosedFunction · 0.90
comparePositionsFunction · 0.90
findMethod · 0.65
clearMethod · 0.65

Tested by

no test coverage detected