<commit_before>/*
 * FoldingProvider
 *
 * Super basic highlight provider
 */

import { SyntaxNode, Tree } from 'tree-sitter';
import {
	FoldingRange,
	FoldingRangeKind,
	FoldingRangeRequest,
	FoldingRangeRequestParam,
	IConnection,
} from 'vscode-languageserver';
import { IForest } from '../Forest';
import Provider from './Provider';

export default class FoldingRangeProvider extends Provider {
	private readonly FOLD_CONSTRUCTS: Set<string> = new Set([
		'begin',
		'def',
		'if',
		'case',
		'unless',
		'do',
		'class',
		'module',
	]);

	static register(connection: IConnection, forest: IForest) {
		return new FoldingRangeProvider(connection, forest);
	}

	constructor(connection: IConnection, forest: IForest) {
		super(connection, forest);

		this.connection.onRequest(FoldingRangeRequest.type, this.handleFoldingRange);
	}

	protected handleFoldingRange = async (
		param: FoldingRangeRequestParam
	): Promise<FoldingRange[]> => {
		const folds: FoldingRange[] = [];

		const tree: Tree = this.forest.getTree(param.textDocument.uri);

		const traverse: (node: SyntaxNode) => void = (node: SyntaxNode): void => {
			if (!node.isNamed && this.FOLD_CONSTRUCTS.has(node.type)) {
				const endNode: SyntaxNode = node.parent.lastChild;
				folds.push({
					startLine: node.startPosition.row,
					startCharacter: node.startPosition.column,
					endLine: endNode.endPosition.row,
					endCharacter: node.endPosition.column,
					kind: FoldingRangeKind.Region,
				});
			}
			for (const childNode of node.children) {
				traverse(childNode);
			}
		};
		traverse(tree.rootNode);

		return folds;
	};
}<commit_msg>Have FoldingRangeProvider use the last analysis<commit_after>/*
 * FoldingRangeProvider
 */

import {
	FoldingRange,
	FoldingRangeRequest,
	FoldingRangeParams,
	IConnection,
} from 'vscode-languageserver';
import Provider from './Provider';
import { analyses } from '../Analyzer';

export default class FoldingRangeProvider extends Provider {
	static register(connection: IConnection) {
		return new FoldingRangeProvider(connection);
	}

	constructor(connection: IConnection) {
		super(connection);

		this.connection.onRequest(FoldingRangeRequest.type, this.handleFoldingRange);
	}

	protected handleFoldingRange = async (params: FoldingRangeParams): Promise<FoldingRange[]> => {
		const {
			textDocument: { uri },
		} = params;
		const analysis = analyses.getAnalysis(uri);
		return analysis.foldingRanges;
	};
}
{'fraction_non_alphanumeric': 0.12078882497945768, 'fraction_numerical': 0.0, 'mean_word_length': 4.729411764705882, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, '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': '5926806', 'n_tokens_mistral': 839, 'n_tokens_neox': 745, 'n_words': 166}