export function findLineOfFirstCode()

in packages/core/src/codewhisperer/util/importAdderUtil.ts [18:54]


export function findLineOfFirstCode(editor: vscode.TextEditor, firstLineOfRecommendation: number): number {
    const lang = editor.document.languageId
    for (let i = 0; i <= firstLineOfRecommendation; i++) {
        const text = editor.document.lineAt(i).text
        if (lang === 'python') {
            // skip #, empty line
            if (!text.match(/^\s*#/) && !text.match(/^\s*$/)) {
                return i
            }
        } else if (lang === 'javascript' || lang === 'jsx') {
            // skip //, /*, *, */, empty line
            if (
                !text.match(/^\s*\/\//) &&
                !text.match(/\s*use\s+strict/) &&
                !text.match(/^\s*$/) &&
                !text.match(/^\s*\/\s*\*/) &&
                !text.match(/^\s*\*/) &&
                !text.match(/^\s*\*\s*\//)
            ) {
                return i
            }
        } else if (lang === 'java') {
            // skip //, /*, *, */, package, empty line
            if (
                !text.match(/^\s*\/\//) &&
                !text.match(/^\s*package\s+\S+/) &&
                !text.match(/^\s*$/) &&
                !text.match(/^\s*\/\s*\*/) &&
                !text.match(/^\s*\*/) &&
                !text.match(/^\s*\*\s*\//)
            ) {
                return i
            }
        }
    }
    return 0
}