Skip to content

Commit 03e47b0

Browse files
author
Deep Furiya
committed
rev2 addressed comments
1 parent e9f8298 commit 03e47b0

File tree

4 files changed

+41
-46
lines changed

4 files changed

+41
-46
lines changed

src/document/Document.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ export class Document {
1717

1818
constructor(
1919
public readonly uri: DocumentUri,
20-
private readonly textDocument: (uri: string) => TextDocument,
20+
private readonly textDocument: (uri: string) => TextDocument | undefined,
2121
detectIndentation: boolean = true,
2222
fallbackTabSize: number = DefaultSettings.editor.tabSize,
2323
) {
24-
const doc = this.textDocument(uri);
25-
const { extension, type } = detectDocumentType(doc.uri, doc.getText());
24+
const doc = this.getTextDocument();
25+
const { extension, type } = doc
26+
? detectDocumentType(doc.uri, doc.getText())
27+
: { extension: '', type: DocumentType.YAML };
2628

2729
this.extension = extension;
2830
this.documentType = type;
@@ -34,24 +36,28 @@ export class Document {
3436
this.processIndentation(detectIndentation, fallbackTabSize);
3537
}
3638

37-
public get languageId(): string {
38-
return this.textDocument(this.uri).languageId;
39+
private getTextDocument(): TextDocument | undefined {
40+
return this.textDocument(this.uri);
3941
}
4042

41-
public get version(): number {
42-
return this.textDocument(this.uri).version;
43+
public get languageId(): string | undefined {
44+
return this.getTextDocument()?.languageId;
4345
}
4446

45-
public get lineCount(): number {
46-
return this.textDocument(this.uri).lineCount;
47+
public get version(): number | undefined {
48+
return this.getTextDocument()?.version;
49+
}
50+
51+
public get lineCount(): number | undefined {
52+
return this.getTextDocument()?.lineCount;
4753
}
4854

4955
public get cfnFileType(): CloudFormationFileType {
5056
return this._cfnFileType;
5157
}
5258

5359
public updateCfnFileType(): void {
54-
const content = this.textDocument(this.uri).getText();
60+
const content = this.getTextDocument()?.getText() ?? '';
5561
if (!content.trim()) {
5662
this._cfnFileType = CloudFormationFileType.Empty;
5763
this.cachedParsedContent = undefined;
@@ -69,7 +75,7 @@ export class Document {
6975
}
7076

7177
private parseContent(): unknown {
72-
const content = this.textDocument(this.uri).getText();
78+
const content = this.getTextDocument()?.getText() ?? '';
7379
if (this.documentType === DocumentType.JSON) {
7480
return JSON.parse(content);
7581
}
@@ -132,27 +138,27 @@ export class Document {
132138
}
133139

134140
public getText(range?: Range) {
135-
return this.textDocument(this.uri).getText(range);
141+
return this.getTextDocument()?.getText(range) ?? '';
136142
}
137143

138144
public getLines(): string[] {
139145
return this.getText().split('\n');
140146
}
141147

142148
public positionAt(offset: number) {
143-
return this.textDocument(this.uri).positionAt(offset);
149+
return this.getTextDocument()?.positionAt(offset);
144150
}
145151

146152
public offsetAt(position: Position) {
147-
return this.textDocument(this.uri).offsetAt(position);
153+
return this.getTextDocument()?.offsetAt(position);
148154
}
149155

150156
public isTemplate() {
151157
return this.cfnFileType === CloudFormationFileType.Template;
152158
}
153159

154160
public contents() {
155-
return this.textDocument(this.uri).getText();
161+
return this.getTextDocument()?.getText() ?? '';
156162
}
157163

158164
public metadata(): DocumentMetadata {
@@ -162,9 +168,9 @@ export class Document {
162168
ext: this.extension,
163169
type: this.documentType,
164170
cfnType: this.cfnFileType,
165-
languageId: this.languageId,
166-
version: this.version,
167-
lineCount: this.lineCount,
171+
languageId: this.languageId ?? '',
172+
version: this.version ?? 0,
173+
lineCount: this.lineCount ?? 0,
168174
};
169175
}
170176

src/document/DocumentManager.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,19 @@ export class DocumentManager implements SettingsConfigurable, Closeable {
4646
}
4747

4848
get(uri: string) {
49-
let document = this.documentMap.get(uri);
50-
if (document) {
51-
return document;
52-
}
53-
5449
const textDocument = this.documents.get(uri);
5550
if (!textDocument) {
5651
return;
5752
}
5853

54+
let document = this.documentMap.get(uri);
55+
if (document) {
56+
return document;
57+
}
58+
5959
document = new Document(
6060
uri,
61-
(u) => {
62-
const doc = this.documents.get(u);
63-
if (!doc) {
64-
throw new Error(`TextDocument not found for uri: ${u}`);
65-
}
66-
return doc;
67-
},
61+
(u) => this.documents.get(u),
6862
this.editorSettings.detectIndentation,
6963
this.editorSettings.tabSize,
7064
);
@@ -86,13 +80,7 @@ export class DocumentManager implements SettingsConfigurable, Closeable {
8680
if (!document) {
8781
document = new Document(
8882
textDoc.uri,
89-
(u) => {
90-
const doc = this.documents.get(u);
91-
if (!doc) {
92-
throw new Error(`TextDocument not found for uri: ${u}`);
93-
}
94-
return doc;
95-
},
83+
(u) => this.documents.get(u),
9684
this.editorSettings.detectIndentation,
9785
this.editorSettings.tabSize,
9886
);

src/resourceState/ResourceStateImporter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export class ResourceStateImporter {
114114
if (insertPosition.replaceEntireFile) {
115115
// Replace entire file with properly formatted JSON
116116
snippetText = docFormattedText;
117-
const endPosition = { line: document.lineCount, character: 0 };
117+
const endPosition = { line: document.lineCount ?? 0, character: 0 };
118118
textEdit = TextEdit.replace(Range.create({ line: 0, character: 0 }, endPosition), snippetText);
119119
} else {
120120
// Insert at specific position
@@ -420,7 +420,7 @@ export class ResourceStateImporter {
420420
: { line: resourcesSection.endPosition.row + 1, character: 0 };
421421
} else {
422422
// Find the last non-empty line
423-
let lastNonEmptyLine = document.lineCount - 1;
423+
let lastNonEmptyLine = (document.lineCount ?? 1) - 1;
424424
while (lastNonEmptyLine >= 0 && document.getLine(lastNonEmptyLine)?.trim().length === 0) {
425425
lastNonEmptyLine--;
426426
}
@@ -434,7 +434,7 @@ export class ResourceStateImporter {
434434
};
435435
}
436436

437-
let line = resourcesSection ? resourcesSection.endPosition.row : document.lineCount - 1;
437+
let line = resourcesSection ? resourcesSection.endPosition.row : (document.lineCount ?? 1) - 1;
438438

439439
// For JSON without Resources section, check if file is essentially empty
440440
if (!resourcesSection) {
@@ -495,7 +495,7 @@ export class ResourceStateImporter {
495495
}
496496
// malformed case, allow import to end of document
497497
return {
498-
position: { line: document.lineCount, character: 0 },
498+
position: { line: document.lineCount ?? 0, character: 0 },
499499
commaPrefixNeeded: false,
500500
newLineSuffixNeeded: false,
501501
replaceEntireFile: false,

src/utils/ResourceInsertionUtils.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ export function getInsertPosition(resourcesSection: SyntaxNode | undefined, docu
2929
? { line: resourcesSection.endPosition.row, character: 0 }
3030
: { line: resourcesSection.endPosition.row + 1, character: 0 };
3131
} else {
32+
const lineCount = document.lineCount ?? 1;
3233
position =
33-
document.getLine(document.lineCount - 1)?.trim().length === 0
34-
? { line: document.lineCount - 1, character: 0 }
35-
: { line: document.lineCount, character: 0 };
34+
document.getLine(lineCount - 1)?.trim().length === 0
35+
? { line: lineCount - 1, character: 0 }
36+
: { line: lineCount, character: 0 };
3637
}
3738
return { position, commaPrefixNeeded: false, newLineSuffixNeeded: false };
3839
}
3940

4041
// JSON handling
41-
let line = resourcesSection ? resourcesSection.endPosition.row : document.lineCount - 1;
42+
let line = resourcesSection ? resourcesSection.endPosition.row : (document.lineCount ?? 1) - 1;
4243
while (line > 0) {
4344
const previousLine = document.getLine(line - 1);
4445
if (previousLine === undefined) {
@@ -61,7 +62,7 @@ export function getInsertPosition(resourcesSection: SyntaxNode | undefined, docu
6162
}
6263
// malformed case, allow import to end of document
6364
return {
64-
position: { line: document.lineCount, character: 0 },
65+
position: { line: document.lineCount ?? 0, character: 0 },
6566
commaPrefixNeeded: false,
6667
newLineSuffixNeeded: false,
6768
};

0 commit comments

Comments
 (0)