<commit_before>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import wx
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin

from robotide.pluginapi import Plugin, ActionInfo, RideOpenSuite,\
    RideOpenResource, RideImportSetting, RideUserKeyword
from robotide import utils


class KeywordSearch(Plugin):
    """A plugin for searching keywords based on name or documentation."""

    def __init__(self, app):
        Plugin.__init__(self, app)
        self._all_keywords = []
        self._criteria = _SearchCriteria()

    def enable(self):
        action = ActionInfo('Tools', 'Search Keywords', self.OnSearch,
                            doc='Search keywords from libraries and resources')
        self.register_action(action)
        self.subscribe(self.refresh, RideOpenSuite, RideOpenResource,
                       RideImportSetting, RideUserKeyword)
        self._dialog = KeywordSearchDialog(self.frame, self)

    def OnSearch(self, event):
        if not self._dialog.IsShown():
            self._dialog.Show()

    def refresh(self, message):
        self._all_keywords = self.model.get_all_keywords()

    def search(self, pattern, search_docs):
        self._criteria = _SearchCriteria(pattern, search_docs)
        return self._search()

    def _search(self):
        return [ kw for kw in self._all_keywords if self._criteria.matches(kw) ]


class _SearchCriteria(object):

    def __init__(self, pattern='', search_docs=True):
        self._pattern = pattern
        self._search_docs = search_docs

    def matches(self, kw):
        if self._contains(kw.name, self._pattern):
            return True
        return self._search_docs and self._contains(kw.doc, self._pattern)

    def _contains(self, string, pattern):
        return utils.normalize(pattern) in utils.normalize(string)


class KeywordSearchDialog(wx.Frame):

    def __init__(self, parent, searcher):
        wx.Frame.__init__(self, parent, title="Search Keywords")
        self._searcher = searcher
        self._create_components(searcher)
        self._make_bindings()
        self._sort_up = True
        self._sortcol = 0

    def _create_components(self, searcher):
        self.SetSizer(wx.BoxSizer(wx.VERTICAL))
        self._add_search_control('Filter Names: ')
        self._list = _KeywordList(self)
        self._list.SetSize(self.Size)
        self.Sizer.Add(self._list, 1, wx.EXPAND| wx.ALL, 3)
        self._details = utils.RideHtmlWindow(self)
        self.Sizer.Add(self._details, 1, wx.EXPAND | wx.ALL, 3)
        self.SetSize((700,500))

    def _add_search_control(self, label):
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(wx.StaticText(self, label=label))
        self._search_control = wx.SearchCtrl(self, size=(200,-1),
                                             style=wx.TE_PROCESS_ENTER)
        sizer.Add(self._search_control)
        self._use_doc = wx.CheckBox(self, label='Search Documentation')
        self._use_doc.SetValue(True)<commit_msg>Fixed background color problem on search window<commit_after>#  Copyright 2008-2009 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import wx
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin

from robotide.pluginapi import Plugin, ActionInfo, RideOpenSuite,\
    RideOpenResource, RideImportSetting, RideUserKeyword
from robotide import utils


class KeywordSearch(Plugin):
    """A plugin for searching keywords based on name or documentation."""

    def __init__(self, app):
        Plugin.__init__(self, app)
        self._all_keywords = []
        self._criteria = _SearchCriteria()

    def enable(self):
        action = ActionInfo('Tools', 'Search Keywords', self.OnSearch,
                            doc='Search keywords from libraries and resources')
        self.register_action(action)
        self.subscribe(self.refresh, RideOpenSuite, RideOpenResource,
                       RideImportSetting, RideUserKeyword)
        self._dialog = KeywordSearchDialog(self.frame, self)

    def OnSearch(self, event):
        if not self._dialog.IsShown():
            self._dialog.Show()

    def refresh(self, message):
        self._all_keywords = self.model.get_all_keywords()

    def search(self, pattern, search_docs):
        self._criteria = _SearchCriteria(pattern, search_docs)
        return self._search()

    def _search(self):
        return [ kw for kw in self._all_keywords if self._criteria.matches(kw) ]


class _SearchCriteria(object):

    def __init__(self, pattern='', search_docs=True):
        self._pattern = pattern
        self._search_docs = search_docs

    def matches(self, kw):
        if self._contains(kw.name, self._pattern):
            return True
        return self._search_docs and self._contains(kw.doc, self._pattern)

    def _contains(self, string, pattern):
        return utils.normalize(pattern) in utils.normalize(string)


class KeywordSearchDialog(wx.Frame):

    def __init__(self, parent, searcher):
        wx.Frame.__init__(self, parent, title="Search Keywords")
        self._searcher = searcher
        self._create_components(searcher)
        self._make_bindings()
        self._sort_up = True
        self._sortcol = 0
        self.SetBackgroundColour(wx.NullColour)

    def _create_components(self, searcher):
        self.SetSizer(wx.BoxSizer(wx.VERTICAL))
        self._add_search_control('Filter Names: ')
        self._list = _KeywordList(self)
        self._list.SetSize(self.Size)
        self.Sizer.Add(self._list, 1, wx.EXPAND| wx.ALL, 3)
        self._details = utils.RideHtmlWindow(self)
        self.Sizer.Add(self._details, 1, wx.EXPAND | wx.ALL, 3)
        self.SetSize((700,500))

    def _add_search_control(self, label):
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(wx.StaticText(self, label=label))
        self._search_control = wx.SearchCtrl(self, size=(200,-1),
                                             style=wx.TE_PROCESS_ENTER)
        sizer.Add(self._search_control)
        self._use_doc = wx.CheckBox(self, label='Search Documentation')
        self._use_doc.SetValue(True)
{'fraction_non_alphanumeric': 0.09506551435740172, 'fraction_numerical': 0.007527181488709228, 'mean_word_length': 2.9597130242825607, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3607161', 'n_tokens_mistral': 2114, 'n_tokens_neox': 2040, 'n_words': 536}