예제 #1
0
        private Dictionary <int, string> findQuote(string quote)
        {
            string allLines = FilePeek.Text,
                   line     = "";
            int len         = FilePeek.TextLength,
                lineNumber  = 0;

            if (!detailedSearch.ContainsKey(quote))
            {
                return(null);
            }
            List <int> indices = detailedSearch[quote];
            Dictionary <int, string> searchDetail = new Dictionary <int, string>();

            foreach (int i in indices)
            {
                lineNumber = FilePeek.GetLineFromCharIndex(i);
                line       = FilePeek.Lines.ElementAt(lineNumber);
                if (!searchDetail.ContainsKey(lineNumber))
                {
                    searchDetail.Add(lineNumber, line);
                }
            }
            return(searchDetail);
        }
예제 #2
0
        public void InitFilePeek(string filePath = "")
        {
            string[] allLines = new string[] { };
            detailedSearch.Clear();

            if (filePath == "")
            {
                allLines = Regex.Split(FilePeek.Text, "\n");
            }
            else
            {
                string lastFolder = FolderName;
                FolderName    = Regex.Split(filePath, @"\\").Reverse().ElementAt(1);
                allLines      = File.ReadAllLines(filePath);
                FilePath      = filePath;
                FilePeek.Text = string.Join("\r\n", allLines);
                undoList.Push(FilePeek.Text);
            }

            int[] ASCIIquotes = { 34, 39 };
            Dictionary <int, int> quotationIndices = new Dictionary <int, int>();
            Dictionary <int, int> commentIndices   = new Dictionary <int, int>();
            bool isComment    = false;
            int  commentStart = 0;

            for (int lineNumber = 0; lineNumber < allLines.Length; lineNumber++)
            {
                string line         = allLines[lineNumber];
                string trimmedLine  = line.Trim();
                int    currentIndex = FilePeek.GetFirstCharIndexFromLine(lineNumber);
                if (trimmedLine.EndsWith("*/"))
                {
                    isComment = false;
                    if (!commentIndices.Keys.Contains(commentStart))
                    {
                        commentIndices.Add(commentStart, currentIndex - commentStart + line.Length);
                    }
                    continue;
                }
                else if (isComment == true)
                {
                    continue;
                }
                else if (trimmedLine.StartsWith("//"))
                {
                    commentIndices.Add(currentIndex, line.Length);
                    continue;
                }
                else if (trimmedLine.StartsWith("/*"))
                {
                    isComment    = true;
                    commentStart = currentIndex;
                    continue;
                }
                //else if (trimmedLine.Contains("</script>") && trimmedLine.Contains("</script>"))
                //    continue;
                //else if (trimmedLine.StartsWith("<") && trimmedLine.EndsWith(">"))
                //    continue;
                else if (line.Contains('\"') || line.Contains('\''))
                {
                    //int inLineCommentStart = line.IndexOf("//");
                    //if (inLineCommentStart > 0)
                    //    line = line.Substring(0, inLineCommentStart);
                    char quote           = '\0';
                    int  startquoteIndex = 0;
                    for (int i = 0; i < line.Length; i++)
                    {
                        char character = line[i],
                             nextChar  = i == line.Length - 1 ? '~' : line[i + 1],
                             prevChar  = i == 0 ? '~' : line[i - 1],
                             prevChar2 = i < 2 ? '~' : line[i - 2];
                        int ascii      = (int)character;
                        if (ascii == 92)
                        {
                            i++;
                            if (i > line.Length)
                            {
                                break;
                            }
                        }
                        else if (character == quote)
                        {
                            if (nextChar == ']' || nextChar == ':')
                            {
                                quote = '\0';
                                continue;
                            }
                            else
                            {
                                int key   = currentIndex + startquoteIndex,
                                    value = i - startquoteIndex + 1;
                                quotationIndices.Add(key, value);
                                quote = '\0';
                            }
                        }
                        else if (ASCIIquotes.Contains(ascii))
                        {
                            if (prevChar == '(' && prevChar2 == '$')
                            {
                                continue;
                            }
                            else
                            {
                                quote           = line[i];
                                startquoteIndex = i;
                            }
                        }
                    }
                }
            }

            List <string> exclude = new List <string>(new string[] { "\"\"", "{}", string.Empty });

            foreach (KeyValuePair <int, int> pair in quotationIndices)
            {
                string word = FilePeek.Text.Substring(pair.Key, pair.Value);
                if (!exclude.Contains(word) && word != "")
                {
                    int    lineNumber    = FilePeek.GetLineFromCharIndex(pair.Key);
                    string lineWithQuote = FilePeek.Lines[lineNumber];
                    LinesWIthQuote.Add(lineWithQuote);

                    if (!detailedSearch.ContainsKey(word))
                    {
                        detailedSearch.Add(word, new List <int>()
                        {
                            pair.Key
                        });
                    }
                    else
                    {
                        detailedSearch[word].Add(pair.Key);
                    }
                    FilePeek.Select(pair.Key, pair.Value);
                    FilePeek.SelectionBackColor = Color.PaleVioletRed;
                }
            }

            if (filePath != "")
            {
                this.Text += " - " + detailedSearch.Count + " suspect found!";
            }

            populateResultGrid();

            foreach (KeyValuePair <int, int> pair in commentIndices)
            {
                FilePeek.Select(pair.Key, pair.Value);
                FilePeek.SelectionColor = Color.Gray;
            }

            //FilePeek.WordWrap = true;
            FilePeek.DeselectAll();
        }