Skip to content

Commit

Permalink
Merge pull request #3295 from goanpeca/fix/search
Browse files Browse the repository at this point in the history
PR: Fix/Find in files
  • Loading branch information
ccordoba12 authored Jul 16, 2016
2 parents 78fae52 + ed4d124 commit ca27efc
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
4 changes: 2 additions & 2 deletions spyderlib/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
'search_text': [''],
'search_text_samples': [codeanalysis.TASKS_PATTERN],
'in_python_path': False,
'more_options': True,
'more_options': False,
}),
('workingdir',
{
Expand Down Expand Up @@ -585,7 +585,7 @@
# or if you want to *rename* options, then you need to do a MAJOR update in
# version, e.g. from 3.0.0 to 4.0.0
# 3. You don't need to touch this value if you're just adding a new option
CONF_VERSION = '27.1.0'
CONF_VERSION = '27.2.0'


# XXX: Previously we had load=(not DEV) here but DEV was set to *False*.
Expand Down
18 changes: 13 additions & 5 deletions spyderlib/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,23 @@ def _get_pygments_extensions():
"""Return all file type extensions supported by Pygments"""
# NOTE: Leave this import here to keep startup process fast!
import pygments.lexers as lexers

extensions = []
all_lexers = lexers.get_all_lexers()
for lx in all_lexers:
for lx in lexers.get_all_lexers():
lexer_exts = lx[2]

if lexer_exts:
lexer_exts = [le[1:] for le in lexer_exts]
# Reference: This line was included for leaving untrimmed the
# extensions not starting with `*`
other_exts = [le for le in lexer_exts if not le.startswith('*')]
# Reference: This commented line was replaced by the following one
# to trim only extensions that start with '*'
# lexer_exts = [le[1:] for le in lexer_exts]
lexer_exts = [le[1:] for le in lexer_exts if le.startswith('*')]
lexer_exts = [le for le in lexer_exts if not le.endswith('_*')]
extensions = extensions + list(lexer_exts)
return extensions
extensions = extensions + list(lexer_exts) + list(other_exts)

return sorted(list(set(extensions)))


#==============================================================================
Expand Down
38 changes: 31 additions & 7 deletions spyderlib/plugins/findinfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
# pylint: disable=R0911
# pylint: disable=R0201

# Standard library imports
import sys

# Third party imports
from qtpy.QtWidgets import QApplication
from qtpy.QtCore import Signal, Slot

# Local imports
from spyderlib.config.base import _
from spyderlib.config.utils import get_edit_extensions
from spyderlib.plugins import SpyderPluginMixin
from spyderlib.py3compat import getcwd
from spyderlib.utils import icon_manager as ima
from spyderlib.utils.qthelpers import create_action
from spyderlib.widgets.findinfiles import FindInFilesWidget
from spyderlib.plugins import SpyderPluginMixin


class FindInFiles(FindInFilesWidget, SpyderPluginMixin):
Expand Down Expand Up @@ -94,14 +97,23 @@ def findinfiles_callback(self):
if text:
self.find()

def include_patterns(self):
edit_ext = get_edit_extensions()
patterns = [r'|'.join(['\\'+_ext+r'$' for _ext in edit_ext if _ext])+\
@staticmethod
def include_patterns():
"""Generate regex common usage patterns to include section."""
# Change special characters, like + and . to convert into valid re
clean_exts = []
for ext in get_edit_extensions():
ext = ext.replace('.', r'\.')
ext = ext.replace('+', r'\+')
clean_exts.append(ext)

patterns = [r'|'.join([ext + r'$' for ext in clean_exts if ext]) +
r'|README|INSTALL',
r'\.pyw?$|\.ipy$|\.txt$|\.rst$',
'.']
r'\.ipy$|\.pyw?$|\.rst$|\.txt$',
'.',
]
return patterns

#------ SpyderPluginMixin API ---------------------------------------------
def switch_to_plugin(self):
"""Switch to plugin
Expand Down Expand Up @@ -171,3 +183,15 @@ def closing_plugin(self, cancelable=False):
self.set_option('in_python_path', in_python_path)
self.set_option('more_options', more_options)
return True


def test():
from spyderlib.utils.qthelpers import qapplication
app = qapplication()
widget = FindInFiles()
widget.show()
sys.exit(app.exec_())


if __name__ == '__main__':
test()
44 changes: 44 additions & 0 deletions spyderlib/plugins/tests/test_findinfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright © 2009- The Spyder Development Team
#
# Licensed under the terms of the MIT License
# (see spyderlib/__init__.py for details)
# -----------------------------------------------------------------------------
"""Test scripts for `findinfiles` plugin."""

# Standard library imports
import re

# Local imports
from spyderlib.config.main import EXCLUDE_PATTERNS


class TestFindInFilesPlugin:

def check_regex(self, patterns):
"""
Check that regular expression patterns provided by compiling them.
Return a list of booleans for each of the provided patterns.
"""
checks = []
for pattern in patterns:
try:
re.compile(pattern)
is_valid = True
except re.error:
is_valid = False
checks.append(is_valid)
return checks

def test_include_patterns_are_valid_regex(self, qtbot):
# qtawesome requires a QApplication to exist, so widgets import must
# happen inside the test (or with fixtures)
from spyderlib.plugins.findinfiles import FindInFiles
patterns = FindInFiles.include_patterns()
checks = self.check_regex(patterns)
assert all(checks)

def test_exclude_patterns_are_valid_regex(self):
checks = self.check_regex(EXCLUDE_PATTERNS)
assert all(checks)

0 comments on commit ca27efc

Please sign in to comment.