<commit_before>let vscode = require('vscode');

class Resolver {
    importClass() {
        let activeEditor = this.activeEditor();
        let resolving = this.resolving(activeEditor);

        if (resolving === null) {
            return this.showMessage(`$(issue-opened)  No class is selected.`, true);
        }

        this.findFiles()
            .then(files => this.findNamespaces(activeEditor, resolving, files))
            .then(namespaces => this.pickClass(namespaces))
            .then(pickedClass => {
                let useStatements, declarationLines;

                try {
                    [useStatements, declarationLines] = this.getDeclarations(activeEditor, pickedClass);
                } catch (error) {
                    return this.showMessage(error.message, true);
                }

                if (! this.hasConflict(useStatements, resolving)) {
                    return this.insert(activeEditor, pickedClass, declarationLines);
                }

                vscode.window.showInputBox({
                    placeHolder: 'Enter an alias'
                }).then(alias => {
                    if (alias !== undefined && alias !== '') {
                        this.insert(activeEditor, pickedClass, declarationLines, alias);
                    }
                });
            });
    }

    expandClass() {
        let activeEditor = this.activeEditor();
        let resolving = this.resolving(activeEditor);

        if (resolving === null) {
            return this.showMessage(`$(issue-opened)  No class is selected.`, true);
        }

        this.findFiles()
            .then(files => this.findNamespaces(activeEditor, resolving, files))
            .then(namespaces => this.pickClass(namespaces))
            .then(pickedClass => {
                activeEditor.edit(textEdit => {
                    textEdit.replace(
                        this.getWordRange(activeEditor),
                        (this.config('leadingSeparator') ? '\\' : '') + pickedClass
                    );
                });
            });
    }

    sortImports() {
        try {
            this.sort(this.activeEditor());
        } catch (error) {
            return this.showMessage(error.message, true);
        }

        this.showMessage('$(check)  Imports sorted.');
    }

    findFiles() {
        return vscode.workspace.findFiles('**/*.php', this.config('exclude'));
    }

    findNamespaces(activeEditor, resolving, files) {
        return new Promise((resolve, reject) => {
            let textDocuments = this.getTextDocuments(files, resolving);

            Promise.all(textDocuments).then(docs => {
                let parsedNamespaces = this.parseNamespaces(docs, resolving);

                if (parsedNamespaces.length === 0) {
                    return this.showMessage(`$(circle-slash)  Class ' ${resolving} ' not found.`, true);
                }

                resolve(parsedNamespaces);
            });
        });
    }

    pickClass(namespaces) {
        return new Promise((resolve, reject) => {
            if (namespaces.length === 1) {
                // There is only one namespace found so return with the first namespace.
                return resolve(namespaces[0]);
            }

            vscode.window.showQuickPick(namespaces).then(picked => {
                if (picked !== undefined) {
                    resolve(picked);
                }
            });
        })
    }

    insert(activeEditor, pickedClass, declarationLines, alias = null) {
        let [prepend, append, insertLine] = this.getInsertLine(declarationLines);

        activeEditor.edit(textEdit => {
            textEdit.replace(
                new vscode.Position((insertLine), 0),
                (`${prepend}use ${pickedClass}`) + (alias !== null ? ` as ${alias}` : '') + (`;${append}`)
            );
        });

        if (this.config('autoSort')) {
            activeEditor.document.save().then(() => {
                try {
                    this.sort(activeEditor);
                } catch (error) {
                    // I don't care. LOL
                }
            });
        }

        this.showMessage('$(check)  Class imported.');
    }

    getTextDocuments(files, resolving) {<commit_msg>Show app namespace on top<commit_after>let vscode = require('vscode');

class Resolver {
    importClass() {
        let activeEditor = this.activeEditor();
        let resolving = this.resolving(activeEditor);

        if (resolving === null) {
            return this.showMessage(`$(issue-opened)  No class is selected.`, true);
        }

        this.findFiles()
            .then(files => this.findNamespaces(activeEditor, resolving, files))
            .then(namespaces => this.pickClass(namespaces))
            .then(pickedClass => {
                let useStatements, declarationLines;

                try {
                    [useStatements, declarationLines] = this.getDeclarations(activeEditor, pickedClass);
                } catch (error) {
                    return this.showMessage(error.message, true);
                }

                if (! this.hasConflict(useStatements, resolving)) {
                    return this.insert(activeEditor, pickedClass, declarationLines);
                }

                vscode.window.showInputBox({
                    placeHolder: 'Enter an alias'
                }).then(alias => {
                    if (alias !== undefined && alias !== '') {
                        this.insert(activeEditor, pickedClass, declarationLines, alias);
                    }
                });
            });
    }

    expandClass() {
        let activeEditor = this.activeEditor();
        let resolving = this.resolving(activeEditor);

        if (resolving === null) {
            return this.showMessage(`$(issue-opened)  No class is selected.`, true);
        }

        this.findFiles()
            .then(files => this.findNamespaces(activeEditor, resolving, files))
            .then(namespaces => this.pickClass(namespaces))
            .then(pickedClass => {
                activeEditor.edit(textEdit => {
                    textEdit.replace(
                        this.getWordRange(activeEditor),
                        (this.config('leadingSeparator') ? '\\' : '') + pickedClass
                    );
                });
            });
    }

    sortImports() {
        try {
            this.sort(this.activeEditor());
        } catch (error) {
            return this.showMessage(error.message, true);
        }

        this.showMessage('$(check)  Imports sorted.');
    }

    findFiles() {
        return vscode.workspace.findFiles('**/*.php', this.config('exclude'));
    }

    findNamespaces(activeEditor, resolving, files) {
        return new Promise((resolve, reject) => {
            let textDocuments = this.getTextDocuments(files, resolving);

            Promise.all(textDocuments).then(docs => {
                let parsedNamespaces = this.parseNamespaces(docs, resolving);

                if (parsedNamespaces.length === 0) {
                    return this.showMessage(`$(circle-slash)  Class ' ${resolving} ' not found.`, true);
                }

                resolve(parsedNamespaces);
            });
        });
    }

    pickClass(namespaces) {
        return new Promise((resolve, reject) => {
            if (namespaces.length === 1) {
                // There is only one namespace found so return with the first namespace.
                return resolve(namespaces[0]);
            }

            vscode.window.showQuickPick(namespaces.reverse()).then(picked => {
                if (picked !== undefined) {
                    resolve(picked);
                }
            });
        })
    }

    insert(activeEditor, pickedClass, declarationLines, alias = null) {
        let [prepend, append, insertLine] = this.getInsertLine(declarationLines);

        activeEditor.edit(textEdit => {
            textEdit.replace(
                new vscode.Position((insertLine), 0),
                (`${prepend}use ${pickedClass}`) + (alias !== null ? ` as ${alias}` : '') + (`;${append}`)
            );
        });

        if (this.config('autoSort')) {
            activeEditor.document.save().then(() => {
                try {
                    this.sort(activeEditor);
                } catch (error) {
                    // I don't care. LOL
                }
            });
        }

        this.showMessage('$(check)  Class imported.');
    }

    getTextDocuments(files, resolving) {
{'fraction_non_alphanumeric': 0.12558411214953272, 'fraction_numerical': 0.0009345794392523365, 'mean_word_length': 1.5848429951690821, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '573184', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2090, 'n_words': 406}