MCPcopy Create free account
hub / github.com/Dart-Code/Dart-Code / toggleLineComment

Method toggleLineComment

src/extension/commands/edit.ts:84–150  ·  view source on GitHub ↗
(onlyDartdoc = false)

Source from the content-addressed store, hash-verified

82 }
83
84 private async toggleLineComment(onlyDartdoc = false) {
85 const editor = getActiveRealFileEditor();
86 if (!editor?.selections.length)
87 return;
88 const document = editor.document;
89 const selections = editor.selections;
90
91 // Track the prefix that matches all lines in all selections.
92 // If any line does not start with `///` then it cannot be TRIPLE.
93 // If any line does not start with '//' then it cannot be DOUBLE.
94 // We start from the highest and work down as we find lines that don't match.
95 let commonPrefix: "NONE" | "DOUBLE" | "TRIPLE" = "TRIPLE";
96
97 check: {
98 for (const selection of selections) {
99 for (let lineNumber = selection.start.line; lineNumber <= selection.end.line; lineNumber++) {
100 const line = document.lineAt(lineNumber);
101 // Skip over blank lines, as they won't have comment markers and shouldn't
102 // influence which common prefix we find.
103 if (line.isEmptyOrWhitespace)
104 continue;
105
106 const text = line.text.trim();
107 if (commonPrefix === "TRIPLE" && !text.startsWith("///"))
108 commonPrefix = text.startsWith("//") ? "DOUBLE" : "NONE";
109 else if (commonPrefix === "DOUBLE" && !text.startsWith("//"))
110 commonPrefix = "NONE";
111
112 // Any time we hit NONE, we can bail out.
113 if (commonPrefix === "NONE")
114 break check;
115 }
116 }
117 }
118
119 if (onlyDartdoc) {
120 switch (commonPrefix) {
121 case "NONE":
122 // If no prefix, insert triples.
123 await this.prefixLines(editor, selections, "/// ");
124 break;
125 case "DOUBLE":
126 // If already double, just add the additional one slash.
127 await this.prefixLines(editor, selections, "/");
128 break;
129 case "TRIPLE":
130 // If already triple, remove slashes.
131 await this.removeLinePrefixes(editor, selections, ["/// ", "///"]);
132 break;
133 }
134 } else {
135 switch (commonPrefix) {
136 case "NONE":
137 // If no prefix, insert doubles.
138 await this.prefixLines(editor, selections, "// ");
139 break;
140 case "DOUBLE":
141 // If already double, add an additional slash to make triple.

Callers 1

toggleDartdocCommentMethod · 0.95

Calls 3

prefixLinesMethod · 0.95
removeLinePrefixesMethod · 0.95
getActiveRealFileEditorFunction · 0.90

Tested by

no test coverage detected