Ejemplo n.º 1
0
    public void Build(Properties.CommandInfo commandInfo, Encoding encoding, out string error, out string shellError)
    {
        TextNodeParser buildinParser = null;
        string         command       = commandInfo.command;
        int            index         = command.IndexOf(':');

        if (index != -1)
        {
            customSyntax = command.Substring(index + 1).Trim();
            command      = command.Substring(0, index).Trim();
        }
        foreach (TextNodeParser parser in buildinParsers)
        {
            if (parser.name == command)
            {
                buildinParser = parser;
                break;
            }
        }

        error      = null;
        shellError = null;
        Node node = null;

        if (buildinParser != null)
        {
            node = buildinParser.Parse(buffer.Controller.Lines);
        }
        else
        {
            Process p = new Process();
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError  = true;
            p.StartInfo.RedirectStandardInput  = true;
            p.StartInfo.StandardOutputEncoding = encoding;
            p.StartInfo.StandardErrorEncoding  = encoding;
            p.StartInfo.UseShellExecute        = false;
            p.StartInfo.FileName  = "cmd.exe";
            p.StartInfo.Arguments = "/C " + command;
            p.Start();
            p.StandardInput.Write(buffer.Controller.Lines);
            p.StandardInput.Close();
            string output = p.StandardOutput.ReadToEnd();
            string errors = p.StandardError.ReadToEnd();
            p.WaitForExit();

            if (!string.IsNullOrEmpty(errors))
            {
                shellError = errors;
            }
            else
            {
                try
                {
                    node = new Parser().Load(output);
                }
                catch (System.Exception e)
                {
                    error = "Parsing error: " + e.Message +
                            "\nSee \"" + settings.getTextNodes.name + "\" for more info";
                }
            }
        }
        if (node == null)
        {
            if (error == null)
            {
                error = "Empty output\nSee \"" + settings.getTextNodes.name + "\" for more info";
            }
            return;
        }
        tabSize = settings.tabSize.GetValue(null);
        lines   = Controller.Lines;
        lines.ClearAllUnsafely();
        places = new List <Place>();
        AddLine(buffer.Name, new Place(-1, -1), true);
        AppendNodeOrNodesList(node);
        if (lines.LinesCount == 0)
        {
            lines.AddLineUnsafely(new Line(32));
        }
        else
        {
            lines.CutLastLineBreakUnsafely();
        }
        Place target = buffer.Controller.Lines.SoftNormalizedPlaceOf(buffer.Controller.LastSelection.caret);

        for (int i = places.Count; i-- > 0;)
        {
            if (places[i].iLine <= target.iLine)
            {
                Place place = new Place(0, i);
                if (place.iLine < 0)
                {
                    place.iLine = 0;
                }
                else if (place.iLine >= buffer.Controller.Lines.LinesCount)
                {
                    place.iLine = buffer.Controller.Lines.LinesCount;
                }
                Controller.PutCursor(place, false);
                break;
            }
        }

        showEncoding          = false;
        Controller.isReadonly = true;
        additionKeyMap        = new KeyMap();
        {
            KeyAction action = new KeyAction("&View\\Nodes list\\Close nodes list", DoCloseBuffer, null, false);
            additionKeyMap.AddItem(new KeyItem(Keys.Escape, null, action));
            additionKeyMap.AddItem(new KeyItem(Keys.Control | Keys.OemOpenBrackets, null, action));
        }
        {
            KeyAction action = new KeyAction("&View\\Nodes list\\Jump to node", DoJumpTo, null, false);
            additionKeyMap.AddItem(new KeyItem(Keys.Enter, null, action));
        }
    }
Ejemplo n.º 2
0
    private void Search(string directory, Regex regex, string pattern, bool ignoreCase, string filter, string ignoreDirs)
    {
        positions                    = new List <Position>();
        buffer                       = new Buffer(null, "Find in files results", SettingsMode.Normal);
        buffer.showEncoding          = false;
        buffer.Controller.isReadonly = true;
        lines = buffer.Controller.Lines;
        lines.ClearAllUnsafely();

        bool needCutCurrent = false;

        if (string.IsNullOrEmpty(directory))
        {
            directory      = Directory.GetCurrentDirectory();
            needCutCurrent = true;
        }

        FileSystemScanner scanner = new FileSystemScanner(directory, filter, ignoreDirs);

        scanner.Scan();
        if (scanner.error != null)
        {
            AddLine("Error: File list reading error: " + scanner.error, Ds.Error);
        }
        fsScanComplete = true;

        string currentDirectory       = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
        List <IndexAndLength> indices = new List <IndexAndLength>();
        CompareInfo           ci      = ignoreCase ? CultureInfo.InvariantCulture.CompareInfo : null;
        int    remainsMatchesCount    = 2000000;
        string stopReason             = null;

        foreach (string file in scanner.files)
        {
            if (isStopped)
            {
                if (stopReason == null)
                {
                    stopReason = "STOPPED";
                }
                break;
            }
            string text = null;
            try
            {
                text = File.ReadAllText(file);
            }
            catch (IOException e)
            {
                --remainsMatchesCount;
                if (remainsMatchesCount < 0)
                {
                    isStopped  = true;
                    stopReason = "TOO MANY LINES";
                    break;
                }
                AddLine(file + ": " + e.Message, Ds.Error);
                continue;
            }
            indices.Clear();
            if (regex != null)
            {
                MatchCollection matches = regex.Matches(text);
                if (matches.Count == 0)
                {
                    continue;
                }
                foreach (Match match in matches)
                {
                    indices.Add(new IndexAndLength(match.Index, match.Length));
                }
            }
            else
            {
                int index = ci != null?ci.IndexOf(text, pattern, CompareOptions.IgnoreCase) : text.IndexOf(pattern);

                if (index == -1)
                {
                    continue;
                }
                while (true)
                {
                    indices.Add(new IndexAndLength(index, pattern.Length));
                    index = ci != null?ci.IndexOf(text, pattern, index + 1, CompareOptions.IgnoreCase) : text.IndexOf(pattern, index + 1);

                    if (index == -1)
                    {
                        break;
                    }
                }
            }
            string path = file;
            if (needCutCurrent && path.StartsWith(currentDirectory))
            {
                path = file.Substring(currentDirectory.Length);
            }
            int offset           = 0;
            int currentLineIndex = 0;
            foreach (IndexAndLength indexAndLength in indices)
            {
                int index   = indexAndLength.index;
                int length  = indexAndLength.length;
                int lineEnd = -1;
                while (true)
                {
                    int nIndex = text.IndexOf('\n', offset);
                    int rIndex = text.IndexOf('\r', offset);
                    if (nIndex == -1 && rIndex == -1)
                    {
                        lineEnd = text.Length;
                        break;
                    }
                    int nrIndex = System.Math.Min(nIndex, rIndex);
                    if (nrIndex == -1)
                    {
                        nrIndex = nIndex != -1 ? nIndex : rIndex;
                    }
                    if (nrIndex > index)
                    {
                        lineEnd = nrIndex;
                        break;
                    }
                    currentLineIndex++;
                    if (nrIndex == nIndex)
                    {
                        offset = nIndex + 1;
                    }
                    else
                    {
                        if (rIndex + 1 < text.Length || text[rIndex + 1] == '\n')
                        {
                            offset = rIndex + 2;
                        }
                        else
                        {
                            offset = rIndex + 1;
                        }
                    }
                }

                --remainsMatchesCount;
                if (remainsMatchesCount < 0)
                {
                    isStopped  = true;
                    stopReason = "TOO MANY MATCHES";
                    break;
                }

                int trimOffset      = 0;
                int rightTrimOffset = 0;
                int lineLength      = lineEnd - offset;
                if (index - offset - MaxPartChars > 0)
                {
                    trimOffset = index - offset - MaxPartChars;
                }
                if (lineLength - index + offset - length - MaxPartChars > 0)
                {
                    rightTrimOffset = lineLength - index + offset - length - MaxPartChars;
                }
                if (trimOffset == 0)
                {
                    int whitespaceLength = CommonHelper.GetFirstSpaces(text, offset, lineLength - rightTrimOffset);
                    if (whitespaceLength > 0 && whitespaceLength <= index - offset)
                    {
                        trimOffset = whitespaceLength;
                    }
                }
                positions.Add(new Position(file, new Place(index - offset, currentLineIndex), length));

                int index0  = offset + trimOffset;
                int length0 = lineLength - trimOffset - rightTrimOffset;
                AddLine(path, (currentLineIndex + 1) + " " + (index - offset + 1), text, index0, length0, index - offset - trimOffset, length);
            }
        }
        if (isStopped)
        {
            AddLine("…", Ds.Normal);
            AddLine(stopReason, Ds.Error);
        }
        if (lines.LinesCount == 0)
        {
            lines.AddLineUnsafely(new Line(32));
        }
        else
        {
            lines.CutLastLineBreakUnsafely();
        }
        buffer.additionKeyMap = new KeyMap();
        {
            KeyAction action = new KeyAction("F&ind\\Navigate to found", ExecuteEnter, null, false);
            buffer.additionKeyMap.AddItem(new KeyItem(Keys.Enter, null, action));
            buffer.additionKeyMap.AddItem(new KeyItem(Keys.None, null, action).SetDoubleClick(true));
        }
        finishBuffer = buffer;
        mainForm.Invoke(new Setter(CloseAlert));
    }