11import { Err , Ok , type Result } from "effection" ;
2+ import { indentationEdits } from "./markdown.ts" ;
3+ import type { SourceRange } from "./document-targets.ts" ;
24import { scanSegments } from "./scanner.ts" ;
35import type { ScanDiagnostic , SourceElement , SourceFence , UnfinishedConstruct } from "./scanner.ts" ;
46import { envelopeBody } from "./source-inspection.ts" ;
57import type { Segment } from "./types.ts" ;
68
7- function afterLine ( text : string , offset : number ) : number {
8- const newline = text . indexOf ( "\n" , offset ) ;
9- return newline === - 1 ? text . length : newline + 1 ;
10- }
11-
129function startOfLine ( text : string , offset : number ) : number {
1310 return offset === 0 ? 0 : text . lastIndexOf ( "\n" , offset - 1 ) + 1 ;
1411}
1512
16- function proposeIndentation ( text : string , elements : SourceElement [ ] ) : string {
17- const depthChanges = new Map < number , number > ( ) ;
18- const headerLines = new Set < number > ( ) ;
19- for ( const { opening, closing } of elements ) {
20- const bodyStart = afterLine ( text , opening . end ) ;
21- for (
22- let line = afterLine ( text , opening . start ) ;
23- line < opening . end ;
24- line = afterLine ( text , line )
25- ) {
26- headerLines . add ( line ) ;
27- }
28- if ( closing === undefined ) {
29- continue ;
30- }
31- const beforeTag = text . slice ( startOfLine ( text , opening . start ) , opening . start ) ;
32- const afterTag = text . slice ( opening . end , bodyStart ) ;
33- if ( ! / ^ [ \t ] * $ / . test ( beforeTag ) || ! / ^ [ \t ] * \n $ / . test ( afterTag ) ) {
34- continue ;
35- }
36- const closeLine = startOfLine ( text , closing . start ) ;
37- const beforeClose = text . slice ( closeLine , closing . start ) ;
38- const bodyEnd = / ^ [ \t ] * $ / . test ( beforeClose ) ? closeLine : afterLine ( text , closing . end ) ;
39- if ( bodyStart < bodyEnd ) {
40- depthChanges . set ( bodyStart , ( depthChanges . get ( bodyStart ) ?? 0 ) + 1 ) ;
41- depthChanges . set ( bodyEnd , ( depthChanges . get ( bodyEnd ) ?? 0 ) - 1 ) ;
42- }
43- }
44- let depth = 0 ;
45- const lines = [ ] ;
46- for ( let start = 0 ; start < text . length ; ) {
47- depth += depthChanges . get ( start ) ?? 0 ;
48- const end = afterLine ( text , start ) ;
49- const line = text . slice ( start , end ) ;
50- const prefix = " " . repeat ( depth ) ;
51- if ( headerLines . has ( start ) ) {
52- lines . push ( line ) ;
53- } else if ( line === prefix + "\n" || line === prefix + "\r\n" ) {
54- lines . push ( line . slice ( prefix . length ) ) ;
55- } else {
56- lines . push (
57- line . startsWith ( prefix ) || line === "\n" || line === "\r\n" ? line : prefix + line ,
58- ) ;
59- }
60- start = end ;
13+ function applyEdits ( text : string , edits : Uint32Array ) : string {
14+ const parts : string [ ] = [ ] ;
15+ let cursor = 0 ;
16+ for ( let at = 0 ; at < edits . length ; at += 3 ) {
17+ const start = edits [ at ] ! ;
18+ parts . push ( text . slice ( cursor , start ) , " " . repeat ( edits [ at + 2 ] ! ) ) ;
19+ cursor = start + edits [ at + 1 ] ! ;
6120 }
62- return lines . join ( "" ) ;
21+ return parts . join ( "" ) + text . slice ( cursor ) ;
6322}
6423
6524function scan ( text : string , envelope : string ) {
6625 const elements : SourceElement [ ] = [ ] ;
26+ const blocks : SourceRange [ ] = [ ] ;
6727 const unfinished : UnfinishedConstruct [ ] = [ ] ;
6828 const diagnostics : ScanDiagnostic [ ] = [ ] ;
6929 const fences : SourceFence [ ] = [ ] ;
@@ -81,14 +41,16 @@ function scan(text: string, envelope: string) {
8141 0 ,
8242 diagnostics ,
8343 fences ,
44+ blocks ,
8445 ) ;
8546 const openExecutableFences = new Set (
8647 fences
8748 . filter ( ( { executable, end } ) => executable && end === "unfinished" )
8849 . map ( ( { marker } ) => startOfLine ( text , marker ) ) ,
8950 ) ;
9051 return {
91- elements,
52+ headers : elements . map ( ( { opening } ) => opening ) ,
53+ blocks,
9254 segments,
9355 diagnostic : diagnostics [ 0 ] ,
9456 incomplete : unfinished . find (
@@ -129,17 +91,18 @@ export function formatSource(source: string): Result<string> {
12991 return Err ( new Error ( `Incomplete ${ construct } at ${ line } :${ column } ` ) ) ;
13092 }
13193
132- const formatted = proposeIndentation ( text , before . elements ) ;
133- if ( formatted === text ) {
94+ const edits = indentationEdits ( text , before . blocks , before . headers ) ;
95+ if ( edits . length === 0 ) {
13496 return Ok ( source ) ;
13597 }
98+ const formatted = applyEdits ( text , edits ) ;
13699 const after = scan ( formatted , envelope ) ;
137100 if (
138101 after . diagnostic !== undefined ||
139102 after . incomplete !== undefined ||
140103 JSON . stringify ( withoutPositions ( before . segments ) ) !==
141104 JSON . stringify ( withoutPositions ( after . segments ) ) ||
142- proposeIndentation ( formatted , after . elements ) !== formatted
105+ indentationEdits ( formatted , after . blocks , after . headers ) . length !== 0
143106 ) {
144107 return Ok ( source ) ;
145108 }
0 commit comments