Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/inline/slashDoc.py.stest.ts
13388 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { guessIndentation } from '../../src/extension/prompt/node/indentationGuesser';
7
8
9
export function validateDocstringFormat(fileContents: string, targetLineString: string): void {
10
const lines = fileContents.split('\n');
11
const targetLineIndex = lines.findIndex(line => line.includes(targetLineString));
12
13
if (targetLineIndex === -1) {
14
throw new Error('Target line not found in the file contents.');
15
}
16
17
if (targetLineIndex === lines.length - 1) {
18
throw new Error('Target line is the last line of the file. No space for a docstring.');
19
}
20
21
const indentation = guessIndentation(lines, 4, true);
22
const tabSize = indentation.tabSize;
23
const insertSpaces = indentation.insertSpaces;
24
25
const targetLine = lines[targetLineIndex];
26
const targetIndentation = targetLine.match(/^\s*/)?.[0] || '';
27
28
// Check the next line for a docstring start
29
const nextLine = lines[targetLineIndex + 1];
30
const docstringStart = nextLine.trim().match(/^('''|""")/);
31
if (!docstringStart) {
32
throw new Error('No docstring found after the target line.');
33
}
34
35
const docstringIndentation = nextLine.match(/^\s*/)?.[0] || '';
36
37
// Calculate the expected indentation
38
let expectedIndentation: string;
39
if (insertSpaces) {
40
expectedIndentation = targetIndentation + ' '.repeat(tabSize);
41
} else {
42
expectedIndentation = targetIndentation + '\t';
43
}
44
45
// The docstring should have the expected indentation
46
if (docstringIndentation !== expectedIndentation) {
47
throw new Error(`Incorrect docstring indentation. Expected: '${expectedIndentation.replace(/ /g, '·')}', but got: '${docstringIndentation.replace(/ /g, '·')}'`);
48
}
49
}
50
51