Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Bug 1192290 - Factor up splitlines
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Bhatt authored and erikrose committed Jun 29, 2016
1 parent 8ea7197 commit ca85cab
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions dxr/app.py
Expand Up @@ -28,7 +28,7 @@
from dxr.plugins import plugins_named
from dxr.query import Query, filter_menu_items
from dxr.utils import (non_negative_int, decode_es_datetime, DXR_BLUEPRINT,
format_number, append_update, append_by_line, build_offset_map)
format_number, append_update, append_by_line, build_offset_map, split_content_lines)
from dxr.vcs import file_contents_at_rev

# Look in the 'dxr' package for static files, etc.:
Expand Down Expand Up @@ -552,7 +552,7 @@ def rev(tree, revision, path):
# We do some wrapping to mimic the JSON returned by an ES lines query.
return _browse_file(tree,
path,
[{'content': line} for line in contents.splitlines(True)],
[{'content': line} for line in split_content_lines(contents)],
{},
config,
not is_text,
Expand Down
4 changes: 2 additions & 2 deletions dxr/build.py
Expand Up @@ -29,7 +29,7 @@
from dxr.lines import es_lines, finished_tags
from dxr.mime import decode_data, is_binary_image
from dxr.utils import (open_log, deep_update, append_update,
append_update_by_line, append_by_line, bucket)
append_update_by_line, append_by_line, bucket, split_content_lines)
from dxr.vcs import VcsCache


Expand Down Expand Up @@ -450,7 +450,7 @@ def index_file(tree, tree_indexers, path, es, index):
# Index by line if the contents are text and the path is not a symlink.
index_by_line = is_text and not is_link
if index_by_line:
lines = contents.splitlines(True)
lines = split_content_lines(contents)
num_lines = len(lines)
needles_by_line = [{} for _ in xrange(num_lines)]
annotations_by_line = [[] for _ in xrange(num_lines)]
Expand Down
4 changes: 2 additions & 2 deletions dxr/indexers.py
Expand Up @@ -9,7 +9,7 @@

from funcy import group_by, decorator, imapcat

from dxr.utils import build_offset_map
from dxr.utils import build_offset_map, split_content_lines


STRING_PROPERTY = {
Expand Down Expand Up @@ -329,7 +329,7 @@ def _line_offsets(self):
if not self.contains_text():
raise ValueError("Can't get line offsets for a file that isn't"
" text.")
lines = self.contents.splitlines(True) if self.contents is not None else []
lines = split_content_lines(self.contents) if self.contents is not None else []
self._line_offset_list = build_offset_map(lines)
return self._line_offset_list

Expand Down
4 changes: 2 additions & 2 deletions dxr/plugins/core.py
Expand Up @@ -20,7 +20,7 @@
from dxr.plugins import direct_search
from dxr.trigrammer import (regex_grammar, NGRAM_LENGTH, es_regex_filter,
NoTrigrams, PythonRegexVisitor)
from dxr.utils import glob_to_regex
from dxr.utils import glob_to_regex, split_content_lines

__all__ = ['mappings', 'analyzers', 'TextFilter', 'PathFilter', 'FilenameFilter',
'ExtFilter', 'RegexpFilter', 'IdFilter', 'RefFilter']
Expand Down Expand Up @@ -477,7 +477,7 @@ def needles(self):

def needles_by_line(self):
"""Fill out line number and content for every line."""
for number, text in enumerate(self.contents.splitlines(True), 1):
for number, text in enumerate(split_content_lines(self.contents), 1):
yield [('number', number),
('content', text)]

Expand Down
4 changes: 2 additions & 2 deletions dxr/plugins/python/utils.py
Expand Up @@ -2,7 +2,7 @@
import os
import re
from contextlib import contextmanager

from dxr.utils import split_content_lines

# The actual check that Python uses seems to be done in C, but this
# regex was taken from lib2to3.pgen.tokenize.
Expand All @@ -21,7 +21,7 @@ def ast_parse(contents):
u''.join(
# The encoding declaration is only meaningful in the top two lines.
u'\n' if i < 2 and encoding_re.match(line) else line
for i, line in enumerate(contents.splitlines(True))
for i, line in enumerate(split_content_lines(contents))
)
)

Expand Down
3 changes: 2 additions & 1 deletion dxr/plugins/xpidl/indexers.py
Expand Up @@ -3,6 +3,7 @@

import dxr.indexers
from dxr.indexers import iterable_per_line, with_start_and_end, split_into_lines
from dxr.utils import split_content_lines
from dxr.plugins.xpidl.filters import PLUGIN_NAME
from dxr.plugins.xpidl.visitor import IdlVisitor

Expand All @@ -24,7 +25,7 @@ def idl(self):
# Don't try again if we already excepted.
if not self._idl and not self._had_idl_exception:
try:
self._idl = IdlVisitor(self.parser, self.contents, self.contents.splitlines(True),
self._idl = IdlVisitor(self.parser, self.contents, split_content_lines(self.contents),
self.path, self.absolute_path(),
self.plugin_config.include_folders,
self.plugin_config.header_path, self.tree)
Expand Down
4 changes: 4 additions & 0 deletions dxr/utils.py
Expand Up @@ -285,3 +285,7 @@ def is_in(needle, haystack):
def without_ending(ending, string):
"""If ``string`` ends with ``ending``, strip it off."""
return string[:-len(ending)] if string.endswith(ending) else string

def split_content_lines(string):
"""Line breaks are included in the resulting list"""
return string.splitlines(True)

0 comments on commit ca85cab

Please sign in to comment.