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

Method prefixLines

src/extension/commands/edit.ts:152–187  ·  view source on GitHub ↗
(editor: vs.TextEditor, selections: readonly vs.Selection[], prefix: string)

Source from the content-addressed store, hash-verified

150 }
151
152 private async prefixLines(editor: vs.TextEditor, selections: readonly vs.Selection[], prefix: string) {
153 const document = editor.document;
154 // In case we have overlapping selections, keep track of lines we've done.
155 const doneLines = new Set<number>();
156
157 // Find the minimum indent, so we can insert all slashes at the same level even if
158 // there is indented code.
159 let minIndent: number | undefined;
160 for (const selection of selections) {
161 for (let lineNumber = selection.start.line; lineNumber <= selection.end.line; lineNumber++) {
162 const line = document.lineAt(lineNumber);
163 if (line.isEmptyOrWhitespace)
164 continue;
165
166 if (!minIndent || line.firstNonWhitespaceCharacterIndex < minIndent)
167 minIndent = line.firstNonWhitespaceCharacterIndex;
168 }
169 }
170
171 await editor.edit((edit) => {
172 for (const selection of selections) {
173 for (let lineNumber = selection.start.line; lineNumber <= selection.end.line; lineNumber++) {
174 if (doneLines.has(lineNumber))
175 continue;
176 doneLines.add(lineNumber);
177
178 const line = document.lineAt(lineNumber);
179 if (line.isEmptyOrWhitespace)
180 continue;
181
182 const insertionPoint = line.range.start.translate(0, minIndent);
183 edit.insert(insertionPoint, prefix);
184 }
185 }
186 });
187 }
188
189 private async removeLinePrefixes(editor: vs.TextEditor, selections: readonly vs.Selection[], prefixes: string[]) {
190 const document = editor.document;

Callers 1

toggleLineCommentMethod · 0.95

Calls 2

hasMethod · 0.80
addMethod · 0.45

Tested by

no test coverage detected