<commit_before>/* --------------------------------------------------------------------------------------------
 * Copyright (c) <NAME>. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 * ------------------------------------------------------------------------------------------ */
'use strict';

import { ILogger, IGhcModProvider, IGhcMod, GhcModCmdOpts } from './ghcModInterfaces';
import { DocumentUtils } from './utils/document';
import {
    Diagnostic, DiagnosticSeverity, Range, Position, Location
} from 'vscode-languageserver';

let Path = require('path');

export class GhcModProvider implements IGhcModProvider
 {
    private ghcMod: IGhcMod;
    private logger: ILogger;
    private workspaceRoot: string;

    constructor(ghcMod: IGhcMod, workspaceRoot: string, logger: ILogger) {
        this.ghcMod = ghcMod;
        this.workspaceRoot = workspaceRoot;
        this.logger = logger;
    }

    // GHC-MOD COMMANDS
    public doCheck(text: string, uri: string): Promise<Diagnostic[]> {
        return this.ghcMod.runGhcModCommand(<GhcModCmdOpts>{
            command: 'check',
            text: text,
            uri: this.getRelativePath(uri)
        }).then((lines) => {
            return this.parseCheckDiagnostics(lines);
        });
    }

    public getType(text: string, uri: string, position: Position): Promise<string> {
        return this.ghcMod.runGhcModCommand(<GhcModCmdOpts>{
            command: 'type',
            text: text,
            uri: this.getRelativePath(uri),
            args: [(position.line + 1).toString(), (position.character + 1).toString()]
        }).then((lines) => {
            // Returns results starting with most narrow range
            // Return the first valid type for this position
            // 1 9 1 10 "a"
            // 1 1 1 20 "a -> a"
            return lines.reduce((acc, line) => {
                if (acc) {
                    return acc;
                }
                return this.parseTypeInfo(line, position);
            }, '');
        });
    }

    public getInfo(text: string, uri: string, position: Position): Promise<string> {
        return this.ghcMod.runGhcModCommand(<GhcModCmdOpts>{
            command: 'info',
            text: text,
            uri: this.getRelativePath(uri),
            args: [DocumentUtils.getWordAtPosition(text, position)]
        }).then((lines) => {
            let tooltip = lines.join('\n').replace(/-- Defined at (.+?):(\d+):(\d+)/g, '');
            if (tooltip.indexOf('Cannot show info') === -1) {
                return tooltip;
            } else {
                return '';
            }
        });
    }

    public shutdown(): void {
        this.ghcMod.killProcess();
    }<commit_msg>fix a ghc crash bug on mac when word is empty string<commit_after>/* --------------------------------------------------------------------------------------------
 * Copyright (c) <NAME>. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 * ------------------------------------------------------------------------------------------ */
'use strict';

import { ILogger, IGhcModProvider, IGhcMod, GhcModCmdOpts } from './ghcModInterfaces';
import { DocumentUtils } from './utils/document';
import {
    Diagnostic, DiagnosticSeverity, Range, Position, Location
} from 'vscode-languageserver';

let Path = require('path');

export class GhcModProvider implements IGhcModProvider
 {
    private ghcMod: IGhcMod;
    private logger: ILogger;
    private workspaceRoot: string;

    constructor(ghcMod: IGhcMod, workspaceRoot: string, logger: ILogger) {
        this.ghcMod = ghcMod;
        this.workspaceRoot = workspaceRoot;
        this.logger = logger;
    }

    // GHC-MOD COMMANDS
    public doCheck(text: string, uri: string): Promise<Diagnostic[]> {
        return this.ghcMod.runGhcModCommand(<GhcModCmdOpts>{
            command: 'check',
            text: text,
            uri: this.getRelativePath(uri)
        }).then((lines) => {
            return this.parseCheckDiagnostics(lines);
        });
    }

    public getType(text: string, uri: string, position: Position): Promise<string> {
        return this.ghcMod.runGhcModCommand(<GhcModCmdOpts>{
            command: 'type',
            text: text,
            uri: this.getRelativePath(uri),
            args: [(position.line + 1).toString(), (position.character + 1).toString()]
        }).then((lines) => {
            // Returns results starting with most narrow range
            // Return the first valid type for this position
            // 1 9 1 10 "a"
            // 1 1 1 20 "a -> a"
            return lines.reduce((acc, line) => {
                if (acc) {
                    return acc;
                }
                return this.parseTypeInfo(line, position);
            }, '');
        });
    }

    public getInfo(text: string, uri: string, position: Position): Promise<string> {
        let word = DocumentUtils.getWordAtPosition(text, position);
        if(word != "") {
            return this.ghcMod.runGhcModCommand(<GhcModCmdOpts>{
                command: 'info',
                text: text,
                uri: this.getRelativePath(uri),
                args: [word]
            }).then((lines) => {
                let tooltip = lines.join('\n').replace(/-- Defined at (.+?):(\d+):(\d+)/g, '');
                if (tooltip.indexOf('Cannot show info') === -1) {
                    return tooltip;
                } else {
                    return '';
                }
            });
        } else {
            return new Promise((resolve, reject) => {
                resolve('');
            });
        }
                
    }

    public shutdown(): void {
        this.ghcMod.killProcess();
    }
{'fraction_non_alphanumeric': 0.18118169398907105, 'fraction_numerical': 0.004439890710382513, 'mean_word_length': 1.6276357110812023, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 28, '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': '2947432', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1516, 'n_words': 395}