aboutsummaryrefslogtreecommitdiff
path: root/src/utils.js
blob: eddee9490ad4b1301d84d867cf7957ed59407e3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const vscode = require('vscode');
const path = require('path')
const fs = require('fs')


function getModuleName() {
    try {
        let workspacePath = vscode.workspace.workspaceFolders[0].uri.path
        let modPath = path.join(workspacePath, "go.mod")
        let data = fs.readFileSync(modPath).toString()
        let moduleNameReg = new RegExp("(?<=module ).*")
        let moduleName =moduleNameReg.exec(data)

        return moduleName[0]
    } catch (err) {
        vscode.window.showInformationMessage("Could not get module name")
    }
}

function getFileContent(activeEditor) {
    return activeEditor.document.getText()
}

function getImportsRange(documentText) {
    let start = 1;
    let documentLines = documentText.split('\n')
    for (var line of documentLines) {
        if (line.includes('import (')) {
            break;
        }
        start++;
    }

    let end = start;
    for (var line of documentLines.slice(start)) {
        if (line.includes(')')) {
            break;
        }
        end++;
    }

    return {
        end,
        start,
    };
};



module.exports = {
    getModuleName, getFileContent, getImportsRange
};