Skip to content

Commit 4118428

Browse files
committed
More comments
1 parent a66d4c0 commit 4118428

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/languages/elseIfExtractor.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import {
1515
* @returns An extractor that will exctract `else if` branches
1616
*/
1717
export function elseIfExtractor(): SelectionExtractor {
18+
/**
19+
* This extractor pulls out `if (foo) {}` from `if (foo) {} else {}`, ie it
20+
* excludes any child `else` statement if it exists. It will be used as the
21+
* content range, but the removal range will want to include a leading or
22+
* trailing `else` keyword if one exists.
23+
*/
1824
const contentRangeExtractor = childRangeSelector(["else_clause"], [], {
1925
includeUnnamedChildren: true,
2026
});
@@ -24,12 +30,20 @@ export function elseIfExtractor(): SelectionExtractor {
2430

2531
const parent = node.parent;
2632
if (parent?.type !== "else_clause") {
33+
// We have no leading `else` clause; ie we are a standalone `if`
34+
// statement. We may still have our own `else` child, but we are not
35+
// ourselves a branch of a bigger `if` statement.
2736
const alternative = node.childForFieldName("alternative");
2837

2938
if (alternative == null) {
39+
// If we have no nested else clause, and are not part of an else clause
40+
// ourself, then we don't need to remove any leading / trailing `else`
41+
// keyword
3042
return contentRange;
3143
}
3244

45+
// Otherwise, we have no leading `else`, but we do have our own nested
46+
// `else` clause, so we want to remove the leading `else` keyword
3347
const { selection } = contentRange;
3448
return {
3549
selection,
@@ -42,6 +56,8 @@ export function elseIfExtractor(): SelectionExtractor {
4256
};
4357
}
4458

59+
// If we get here, we are part of a bigger `if` statement; extend our
60+
// removal range past our leading `else` keyword.
4561
const { selection } = contentRange;
4662
return {
4763
selection,
@@ -67,6 +83,9 @@ export function elseExtractor(ifNodeType: string): SelectionExtractor {
6783
const nestedElseIfExtractor = elseIfExtractor();
6884

6985
return function (editor: TextEditor, node: SyntaxNode): SelectionWithContext {
86+
// If we are an `else if` statement, then we just run `elseIfExtractor` on
87+
// our nested `if` node. Otherwise we are a simple `else` branch and don't
88+
// need to do anything fancy.
7089
return node.namedChild(0)!.type === ifNodeType
7190
? nestedElseIfExtractor(editor, node.namedChild(0)!)
7291
: simpleSelectionExtractor(editor, node);

0 commit comments

Comments
 (0)