<commit_before>"use strict"

import vscode = require('vscode')
import FactorioApiData from "./FactorioApiData"
import { getLastMatch, keys, assign } from "./utils"

const wordsRegex = /([\w\[\]]+\.[\w\[\]\.]*)/g

export class FactorioAutocomplete implements vscode.CompletionItemProvider {
    constructor(private apiData: FactorioApiData) { }

    public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> {
        return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
            let lineText = document.lineAt(position.line).text
            let lineTillCurrentPosition = lineText.substr(0, position.character)

            let match = getLastMatch(wordsRegex, lineTillCurrentPosition)
            let line = match ? match[1] : ""

            let words = line.split(".")
            words.pop()

            let type = this.apiData.findType(words)

            if (!type || !type.properties) {
                return reject()
            }

            let suggestions = this.toCompletionItems(type.properties)
            return resolve(suggestions)
        })
    }

    private toCompletionItems(types: FactorioTypeMap): vscode.CompletionItem[] {
        return keys(types).map(key => this.toCompletionItem(types[key], <string>key))
    }

    private toCompletionItem(type: FactorioType, key: string): vscode.CompletionItem {
        const { doc, name, mode } = type

        let completionItem = assign(new vscode.CompletionItem(key), {
            detail: type.type,
            documentation: mode ? `${doc}\n\n${mode}` : doc,
            kind: vscode.CompletionItemKind.Property
        })
<commit_msg>simpler and fixes undefined strings when doc is undefined<commit_after>"use strict"

import vscode = require('vscode')
import FactorioApiData from "./FactorioApiData"
import { getLastMatch, keys, assign } from "./utils"

const wordsRegex = /([\w\[\]]+\.[\w\[\]\.]*)/g

export class FactorioAutocomplete implements vscode.CompletionItemProvider {
    constructor(private apiData: FactorioApiData) { }

    public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> {
        return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
            let lineText = document.lineAt(position.line).text
            let lineTillCurrentPosition = lineText.substr(0, position.character)

            let match = getLastMatch(wordsRegex, lineTillCurrentPosition)
            let line = match ? match[1] : ""

            let words = line.split(".")
            words.pop()

            let type = this.apiData.findType(words)

            if (!type || !type.properties) {
                return reject()
            }

            let suggestions = this.toCompletionItems(type.properties)
            return resolve(suggestions)
        })
    }

    private toCompletionItems(types: FactorioTypeMap): vscode.CompletionItem[] {
        return keys(types).map(key => this.toCompletionItem(types[key], <string>key))
    }

    private toCompletionItem(type: FactorioType, key: string): vscode.CompletionItem {
        const { doc, name, mode } = type

        let completionItem = assign(new vscode.CompletionItem(key), {
            detail: type.type,
            documentation: [doc, mode].filter(Boolean).join("\n\n"),
            kind: vscode.CompletionItemKind.Property
        })

{'fraction_non_alphanumeric': 0.1190068493150685, 'fraction_numerical': 0.001141552511415525, 'mean_word_length': 2.8516483516483517, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '6245836', 'n_tokens_mistral': 986, 'n_tokens_neox': 956, 'n_words': 209}