Skip to content

Commit e965b27

Browse files
committed
instead of keeping leading and trailing offsets we just remember the outer bounds
1 parent b3ea5e4 commit e965b27

3 files changed

Lines changed: 27 additions & 26 deletions

File tree

src/slang-nodes/ModifierDefinition.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { FunctionBody } from './FunctionBody.js';
1010

1111
import type * as ast from '@nomicfoundation/slang/ast';
1212
import type { AstPath, Doc, ParserOptions } from 'prettier';
13-
import type { CollectedMetadata, PrintFunction } from '../types.d.ts';
13+
import type {
14+
AstLocation,
15+
CollectedMetadata,
16+
PrintFunction
17+
} from '../types.d.ts';
1418
import type { AstNode } from './types.d.ts';
1519

1620
export class ModifierDefinition extends SlangNode {
@@ -45,14 +49,12 @@ export class ModifierDefinition extends SlangNode {
4549
this.updateMetadata(this.parameters, this.attributes, this.body);
4650

4751
if (!this.parameters) {
48-
const attributesLoc = this.attributes.loc;
49-
const parametersOffset =
50-
attributesLoc.start - attributesLoc.leadingOffset;
51-
const parametersLoc = {
52+
const parametersOffset = this.attributes.loc.outerStart;
53+
const parametersLoc: AstLocation = {
54+
outerStart: parametersOffset,
55+
outerEnd: parametersOffset,
5256
start: parametersOffset,
53-
end: parametersOffset,
54-
leadingOffset: 0,
55-
trailingOffset: 0
57+
end: parametersOffset
5658
};
5759

5860
this.parameters = Object.assign(

src/slang-nodes/SlangNode.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ export abstract class SlangNode {
4444
enclosePeripheralComments = false
4545
) {
4646
if (ast instanceof SlangTerminalNode) {
47-
const offset = collected.offsets.get(ast.id) || 0;
47+
const start = collected.offsets.get(ast.id) || 0;
48+
const end = start + ast.textLength.utf16;
4849
this.loc = {
49-
start: offset,
50-
end: offset + ast.textLength.utf16,
51-
leadingOffset: 0,
52-
trailingOffset: 0
50+
outerStart: start,
51+
outerEnd: end,
52+
start,
53+
end
5354
};
5455
return;
5556
}
@@ -112,10 +113,10 @@ export abstract class SlangNode {
112113
trailingOffset ??= triviaLength;
113114

114115
this.loc = {
116+
outerStart: initialOffset,
117+
outerEnd: offset,
115118
start: initialOffset + leadingOffset,
116-
end: offset - trailingOffset,
117-
leadingOffset,
118-
trailingOffset
119+
end: offset - trailingOffset
119120
};
120121
}
121122

@@ -124,26 +125,24 @@ export abstract class SlangNode {
124125
): void {
125126
const { loc } = this;
126127
// calculate correct loc object
127-
if (loc.leadingOffset === 0) {
128+
if (loc.outerStart === loc.start) {
128129
for (const childNode of childNodes) {
129130
if (childNode === undefined) continue;
130-
const { leadingOffset, start } = childNode.loc;
131+
const { outerStart, start } = childNode.loc;
131132

132-
if (start - leadingOffset === loc.start) {
133-
loc.leadingOffset = leadingOffset;
133+
if (outerStart === loc.start) {
134134
loc.start = start;
135135
break;
136136
}
137137
}
138138
}
139139

140-
if (loc.trailingOffset === 0) {
140+
if (loc.outerEnd === loc.end) {
141141
for (const childNode of reversedIterator(childNodes)) {
142142
if (childNode === undefined) continue;
143-
const { trailingOffset, end } = childNode.loc;
143+
const { outerEnd, end } = childNode.loc;
144144

145-
if (end + trailingOffset === loc.end) {
146-
loc.trailingOffset = trailingOffset;
145+
if (outerEnd === loc.end) {
147146
loc.end = end;
148147
break;
149148
}

src/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ interface Location {
2020
}
2121

2222
interface AstLocation extends Location {
23-
leadingOffset: number;
24-
trailingOffset: number;
23+
outerStart: number;
24+
outerEnd: number;
2525
}
2626

2727
type PrintFunction = (path: AstPath<AstNode>) => Doc;

0 commit comments

Comments
 (0)