<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2010-2012 by <NAME> <<EMAIL>>
#
#
# This software is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this software.  If not, see <http://www.gnu.org/licenses/>.
#

'''Plugins to work with C++ comments'''

import kate
import kate.gui
import re
import textwrap
# TODO Is it really bad to import in such way? (even here?)
from PyKDE4.ktexteditor import KTextEditor

from libkatepate.decorators import *
from libkatepate.common import getCommentStyleForDoc, getTextBlockAroundCursor, getCurrentLineIndentation
from libkatepate import ui, selection
# text processing predicates
from libkatepate import pred
from libkatepate.pred import neg, all_of, any_of


if 'commentar:comment-position' not in kate.configuration:
    kate.configuration['commentar:comment-position'] = int(60)
if 'commentar:comment-threashold' not in kate.configuration:
    kate.configuration['commentar:comment-threashold'] = int(50)

COMMENT_POS = kate.configuration['commentar:comment-position']
COMMENT_THRESHOLD = kate.configuration['commentar:comment-threashold']
BLOCK_ANY_START_SEARCH_RE = re.compile('^\s*#\s*if.*$')
BLOCK_START_SEARCH_RE = re.compile('^\s*#\s*if\s*([01])$')
BLOCK_ELSE_SEARCH_RE = re.compile('^\s*#\s*else.*$')
BLOCK_END_SEARCH_RE = re.compile('^\s*#\s*endif.*$')
BLOCK_ELSE_ENDIF_MATCH_RE = re.compile('^\s*#\s*(endif|else).*$')
BLOCK_START_GET_COND_RE = re.compile('^\s*#\s*(if((n)?def)?)\s+(.*)\s*$')


def extendSelectionToWholeLine(view):
    selectedRange = view.selectionRange()
    if not selectedRange.isEmpty():
        # ... extend selection to whole line, before do smth
        selectedRange.start().setColumn(0)
        if selectedRange.end().column() != 0:
            selectedRange.end().setColumn(0)
            selectedRange.end().setLine(selectedRange.end().line() + 1)
        view.setSelection(selectedRange)

def buildIfEndifMap(document):
    '''
        Build a list of tuples (start, end, elseif, is_comment) for all #if/#elseif/#endif blocks
        in a document. If block contains #endif 3rd element will point its line, -1 otherwise.
        For blocks #if1/#if0 4th element is True, otherwise False.
    '''
    # Make list of ranges of #if*/#endif blocks
    openBlockStack = list()
    blockRanges = list()
    for i in range(0, document.lines()):
        currentLine = str(document.line(i))
        # Is #if block start?<commit_msg>do not use kate.configuration: will add a configuration page later<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2010-2012 by <NAME> <<EMAIL>>
#
#
# This software is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this software.  If not, see <http://www.gnu.org/licenses/>.
#

'''Plugins to work with C++ comments'''

import kate
import kate.gui
import re
import textwrap
# TODO Is it really bad to import in such way? (even here?)
from PyKDE4.ktexteditor import KTextEditor

from libkatepate.decorators import *
from libkatepate.common import getCommentStyleForDoc, getTextBlockAroundCursor, getCurrentLineIndentation
from libkatepate import ui, selection
# text processing predicates
from libkatepate import pred
from libkatepate.pred import neg, all_of, any_of

# TODO Make a configuration page for the following two parameters
COMMENT_POS = 60
COMMENT_THRESHOLD = 50
BLOCK_ANY_START_SEARCH_RE = re.compile('^\s*#\s*if.*$')
BLOCK_START_SEARCH_RE = re.compile('^\s*#\s*if\s*([01])$')
BLOCK_ELSE_SEARCH_RE = re.compile('^\s*#\s*else.*$')
BLOCK_END_SEARCH_RE = re.compile('^\s*#\s*endif.*$')
BLOCK_ELSE_ENDIF_MATCH_RE = re.compile('^\s*#\s*(endif|else).*$')
BLOCK_START_GET_COND_RE = re.compile('^\s*#\s*(if((n)?def)?)\s+(.*)\s*$')


def extendSelectionToWholeLine(view):
    selectedRange = view.selectionRange()
    if not selectedRange.isEmpty():
        # ... extend selection to whole line, before do smth
        selectedRange.start().setColumn(0)
        if selectedRange.end().column() != 0:
            selectedRange.end().setColumn(0)
            selectedRange.end().setLine(selectedRange.end().line() + 1)
        view.setSelection(selectedRange)

def buildIfEndifMap(document):
    '''
        Build a list of tuples (start, end, elseif, is_comment) for all #if/#elseif/#endif blocks
        in a document. If block contains #endif 3rd element will point its line, -1 otherwise.
        For blocks #if1/#if0 4th element is True, otherwise False.
    '''
    # Make list of ranges of #if*/#endif blocks
    openBlockStack = list()
    blockRanges = list()
    for i in range(0, document.lines()):
        currentLine = str(document.line(i))
        # Is #if block start?
{'fraction_non_alphanumeric': 0.11680508923838134, 'fraction_numerical': 0.009542321965011487, 'mean_word_length': 4.1454545454545455, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6876880', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1725, 'n_words': 566}