Ejemplo n.º 1
0
        public List <LineInfo> GetMatchesInFilePeek(string path, int lineNumber, int peekSize,
                                                    IContentSelector contentSelector)
        {
            CachedFileContent cache = LoadFile(path);

            return(GetMatchesInContent(cache, contentSelector, lineNumber - peekSize, lineNumber + peekSize));
        }
Ejemplo n.º 2
0
        public ResultPeekViewModel(SearchResult searchResult, IContentSelector contentSelector, FileService fileService)
        {
            _fileService          = fileService;
            SearchResult          = searchResult;
            ContentSelector       = contentSelector;
            OpenFileCommand       = new RelayCommand(OpenFile, () => !FileOpened);
            OpenFileInCodeCommand = new RelayCommand(OpenFileInCode);
            CopyFilePathCommand   = new RelayCommand(CopyFilePath);

            _cancellationTokenSource = new CancellationTokenSource();
            Task.Run(() =>
            {
                _cancellationTokenSource.Token.ThrowIfCancellationRequested();
                List <LineInfo> lines = _fileService.GetMatchesInFilePeek(FileName, SearchResult.LineNumber, 10, ContentSelector);
                DispatcherHelper.UIDispatcher.Invoke(() => FileLines = lines);
            }, _cancellationTokenSource.Token);
        }
Ejemplo n.º 3
0
        private IEnumerable <SearchResult> SearchContentInFile(FileInfo file, IContentSelector contentSelector)
        {
            int lineNumber = 1;

            foreach (string line in File.ReadLines(file.FullName))
            {
                if (_cancellationTokenSource.IsCancellationRequested)
                {
                    yield break;
                }

                List <MatchPosition> matches = contentSelector.GetMatches(line).ToList();

                if (matches.Count > 0)
                {
                    yield return(new SearchResult(file, lineNumber, line, file.LastWriteTime.Ticks, matches));
                }

                ++lineNumber;
            }
        }
Ejemplo n.º 4
0
        private List <LineInfo> GetMatchesInContent(CachedFileContent fileContent, IContentSelector contentSelector, int lineMin, int lineMax)
        {
            List <LineInfo> lineInfos = new List <LineInfo>();
            var             index     = 1;

            foreach (string line in fileContent.Content)
            {
                if (index >= lineMin && index <= lineMax)
                {
                    LineInfo lineInfo = fileContent.GetCachedLinfoForLine(index - 1);
                    if (lineInfo == null)
                    {
                        lineInfo = new LineInfo(line, index, contentSelector.GetMatches(line).OrderBy(l => l.Begin).ToList());
                        fileContent.SetCachedLinfoForLine(index - 1, lineInfo);
                    }

                    lineInfos.Add(lineInfo);
                }
                ++index;
            }

            return(lineInfos);
        }
Ejemplo n.º 5
0
        private void SearchContentInFolder(DirectoryInfo directory, IFileSelector fileSelector, IContentSelector contentSelector)
        {
            _forceCancellationTokenSource.Token.ThrowIfCancellationRequested();

            if (_cancellationTokenSource.IsCancellationRequested)
            {
                return;
            }

            List <Task> tasks = new List <Task>();

            foreach (DirectoryInfo subDirectory in directory.GetDirectories())
            {
                tasks.Add(Task.Run(() => SearchContentInFolder(subDirectory, fileSelector, contentSelector)));
            }

            foreach (FileInfo file in directory.GetFiles())
            {
                if (_cancellationTokenSource.IsCancellationRequested)
                {
                    return;
                }

                if (!fileSelector.IsFileValid(file))
                {
                    continue;
                }

                foreach (SearchResult result in SearchContentInFile(file, contentSelector))
                {
                    _results.Enqueue(result);
                }
            }

            foreach (Task task in tasks)
            {
                task.Wait();
            }
        }
Ejemplo n.º 6
0
        public IEnumerable <SearchResult> SearchContentInFolder(string directoryPath, IFileSelector fileSelector, IContentSelector contentSelector)
        {
            DirectoryInfo directory = new DirectoryInfo(directoryPath);

            if (!directory.Exists)
            {
                _completed = true;
                return(Enumerable.Empty <SearchResult>());
            }

            _cancellationTokenSource      = new CancellationTokenSource();
            _forceCancellationTokenSource = new CancellationTokenSource();
            _completed = false;
            Task.Run(() =>
            {
                _forceCancellationTokenSource.Token.ThrowIfCancellationRequested();
                SearchContentInFolder(directory, fileSelector, contentSelector);
                _completed = true;
            });

            return(this);
        }
Ejemplo n.º 7
0
        public List <LineInfo> GetMatchesInFile(string path, IContentSelector contentSelector)
        {
            CachedFileContent cache = LoadFile(path);

            return(GetMatchesInContent(cache, contentSelector, 1, cache.Content.Count));
        }
Ejemplo n.º 8
0
 public SearchEvent(SearchResult selectedResult, IContentSelector contentSelector)
 {
     SelectedResult  = selectedResult;
     ContentSelector = contentSelector;
 }