<commit_before>/**
 * SyntaxHighlighting.ts
 *
 * Handles enhanced syntax highlighting
 */

import * as throttle from "lodash/throttle"

import { configuration, Configuration } from "./../Configuration"

import { editorManager } from "./../EditorManager"
import { HighlightGroupId, HighlightInfo } from "./Definitions"
import { ISyntaxHighlightLineInfo, ISyntaxHighlightState, ISyntaxHighlightTokenInfo } from "./SyntaxHighlightingStore"

import * as Selectors from "./SyntaxHighlightSelectors"

import { Store, Unsubscribe } from "redux"

import * as Log from "./../../Log"

// SyntaxHighlightReconciler
//
// Essentially a renderer / reconciler, that will push
// highlight calls to the active buffer based on the active
// window and viewport
export class SyntaxHighlightReconciler {

    private _unsubscribe: Unsubscribe

    private _previousState: { [line: number]: ISyntaxHighlightLineInfo} = {}

    constructor(
        private _store: Store<ISyntaxHighlightState>,
        private _configuration: Configuration = configuration,
    ) {

        this._unsubscribe = this._store.subscribe(throttle(() => {

            Log.info("[SyntaxHighlightReconciler] Got state update.")

            const state = this._store.getState()

            const activeBuffer: any = editorManager.activeEditor.activeBuffer

            if (!activeBuffer) {
                return
            }

            const bufferId = activeBuffer.id

            const currentHighlightState = state.bufferToHighlights[bufferId]

            if (currentHighlightState && currentHighlightState.lines) {
                const lineNumbers = Object.keys(currentHighlightState.lines)

                const relevantRange = Selectors.getRelevantRange(state, bufferId)

                const filteredLines = lineNumbers.filter((line) => {
                    const lineNumber = parseInt(line, 10)

                    // Ignore lines that are not in current view
                    if (lineNumber < relevantRange.top
                        || lineNumber > relevantRange.bottom) {
                        return false
                    }

                    const latestLine = currentHighlightState.lines[line]

                    // If dirty (haven't processed tokens yet) - skip
                    if (latestLine.dirty) {
                        return false
                    }

                    // Or lines that haven't been updated
                    return this._previousState[line] !== currentHighlightState.lines[line]
                })

                const tokens = filteredLines.map((li) => {
                    const line = currentHighlightState.lines[li]

                    const highlights = this._mapTokensToHighlights(line.tokens)
                    return {
                        line: parseInt(li, 10),
                        highlights,
                    }
                })

                filteredLines.forEach((li) => {
                    this._previousState[li] = currentHighlightState.lines[li]
                })

                if (tokens.length > 0) {
                    Log.info("[SyntaxHighlightReconciler] Applying changes to " + tokens.length + " lines.")
                    activeBuffer.updateHighlights((highlightUpdater: any) => {
                        tokens.forEach((token) => {
                            const line = token.line
                            const highlights = token.highlights

                            if (Log.isDebugLoggingEnabled()) {
                                Log.debug("[SyntaxHighlightingReconciler] Updating tokens for line: " + token.line + " | " + JSON.stringify(highlights))
                            }

                            highlightUpdater.setHighlightsForLine(line, highlights)
                        })
                    })
                }
            }<commit_msg>Remove some extraneous logging (#1045)<commit_after>/**
 * SyntaxHighlighting.ts
 *
 * Handles enhanced syntax highlighting
 */

import * as throttle from "lodash/throttle"

import { configuration, Configuration } from "./../Configuration"

import { editorManager } from "./../EditorManager"
import { HighlightGroupId, HighlightInfo } from "./Definitions"
import { ISyntaxHighlightLineInfo, ISyntaxHighlightState, ISyntaxHighlightTokenInfo } from "./SyntaxHighlightingStore"

import * as Selectors from "./SyntaxHighlightSelectors"

import { Store, Unsubscribe } from "redux"

import * as Log from "./../../Log"

// SyntaxHighlightReconciler
//
// Essentially a renderer / reconciler, that will push
// highlight calls to the active buffer based on the active
// window and viewport
export class SyntaxHighlightReconciler {

    private _unsubscribe: Unsubscribe

    private _previousState: { [line: number]: ISyntaxHighlightLineInfo} = {}

    constructor(
        private _store: Store<ISyntaxHighlightState>,
        private _configuration: Configuration = configuration,
    ) {

        this._unsubscribe = this._store.subscribe(throttle(() => {

            const state = this._store.getState()

            const activeBuffer: any = editorManager.activeEditor.activeBuffer

            if (!activeBuffer) {
                return
            }

            const bufferId = activeBuffer.id

            const currentHighlightState = state.bufferToHighlights[bufferId]

            if (currentHighlightState && currentHighlightState.lines) {
                const lineNumbers = Object.keys(currentHighlightState.lines)

                const relevantRange = Selectors.getRelevantRange(state, bufferId)

                const filteredLines = lineNumbers.filter((line) => {
                    const lineNumber = parseInt(line, 10)

                    // Ignore lines that are not in current view
                    if (lineNumber < relevantRange.top
                        || lineNumber > relevantRange.bottom) {
                        return false
                    }

                    const latestLine = currentHighlightState.lines[line]

                    // If dirty (haven't processed tokens yet) - skip
                    if (latestLine.dirty) {
                        return false
                    }

                    // Or lines that haven't been updated
                    return this._previousState[line] !== currentHighlightState.lines[line]
                })

                const tokens = filteredLines.map((li) => {
                    const line = currentHighlightState.lines[li]

                    const highlights = this._mapTokensToHighlights(line.tokens)
                    return {
                        line: parseInt(li, 10),
                        highlights,
                    }
                })

                filteredLines.forEach((li) => {
                    this._previousState[li] = currentHighlightState.lines[li]
                })

                if (tokens.length > 0) {
                    Log.verbose("[SyntaxHighlightReconciler] Applying changes to " + tokens.length + " lines.")
                    activeBuffer.updateHighlights((highlightUpdater: any) => {
                        tokens.forEach((token) => {
                            const line = token.line
                            const highlights = token.highlights

                            if (Log.isDebugLoggingEnabled()) {
                                Log.debug("[SyntaxHighlightingReconciler] Updating tokens for line: " + token.line + " | " + JSON.stringify(highlights))
                            }

                            highlightUpdater.setHighlightsForLine(line, highlights)
                        })
                    })
                }
            }
{'fraction_non_alphanumeric': 0.08477666362807658, 'fraction_numerical': 0.0018231540565177757, 'mean_word_length': 1.657439446366782, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, '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': '3835677', 'n_tokens_mistral': 1873, 'n_tokens_neox': 1686, 'n_words': 418}