Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Fix/Find in files #3295

Merged
merged 6 commits into from
Jul 16, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do these changes do exactly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lexer_exts = [le[1:] for le in lexer_exts]

This code is wrong cause not all lexers extensions start with a *, so I fixed that so the actual generated patterns are valid regex. (this was causing find in files to not work after a clean install)



#==============================================================================
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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this blank :-)

#------ 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)