Ejemplo n.º 1
0
        /// <summary>
        /// Inserts the specified snippet to the document
        /// </summary>
        public static Int32 InsertSnippetText(ScintillaNet.ScintillaControl sci, Int32 currentPosition, String snippet)
        {
            sci.BeginUndoAction();
            try
            {
                Int32  newIndent;
                String text = snippet;
                if (sci.SelTextSize > 0)
                {
                    currentPosition -= sci.MBSafeTextLength(sci.SelText);
                }
                Int32 line   = sci.LineFromPosition(currentPosition);
                Int32 indent = sci.GetLineIndentation(line);
                sci.ReplaceSel("");

                Int32  lineMarker = LineEndDetector.DetectNewLineMarker(text, sci.EOLMode);
                String newline    = LineEndDetector.GetNewLineMarker(lineMarker);
                if (newline != "\n")
                {
                    text = text.Replace(newline, "\n");
                }
                newline = LineEndDetector.GetNewLineMarker((Int32)PluginBase.MainForm.Settings.EOLMode);
                text    = PluginBase.MainForm.ProcessArgString(text).Replace(newline, "\n");
                newline = LineEndDetector.GetNewLineMarker(sci.EOLMode);
                String[] splitted = text.Trim().Split('\n');
                for (Int32 j = 0; j < splitted.Length; j++)
                {
                    if (j != splitted.Length - 1)
                    {
                        sci.InsertText(sci.CurrentPos, splitted[j] + newline);
                    }
                    else
                    {
                        sci.InsertText(sci.CurrentPos, splitted[j]);
                    }
                    sci.CurrentPos += sci.MBSafeTextLength(splitted[j]) + newline.Length;
                    if (j > 0)
                    {
                        line      = sci.LineFromPosition(sci.CurrentPos - newline.Length);
                        newIndent = sci.GetLineIndentation(line) + indent;
                        sci.SetLineIndentation(line, newIndent);
                    }
                }
                Int32 length = sci.CurrentPos - currentPosition - newline.Length;
                Int32 delta  = PostProcessSnippets(sci, currentPosition);
                return(length + delta);
            }
            finally
            {
                sci.EndUndoAction();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Convert multibyte column to byte length
        /// </summary>
        private int MBSafeColumn(ScintillaNet.ScintillaControl sci, int line, int length)
        {
            String text = sci.GetLine(line) ?? "";

            length = Math.Min(length, text.Length);
            return(sci.MBSafeTextLength(text.Substring(0, length)));
        }
Ejemplo n.º 3
0
        public static string ValidateSource(string fileName, string src, ScintillaNet.ScintillaControl sci)
        {
            Stream str = new MemoryStream(Encoding.Default.GetBytes(src));

            // model generator
            AS3Lexer lexer = new AS3Lexer(str);

            lexer.setFilename(fileName);
            AS3Parser parser = new AS3Parser(lexer);

            parser.setFilename(fileName);
            // start parsing
            try
            {
                parser.compilationUnit();
            }
            catch (RecognitionException rex)
            {
                string result = fileName + ":" + rex.line + ": ";
                string line   = (rex.line > 0) ? sci.GetLine(rex.line - 1) : "";
                int    col    = rex.column;
                if (sci != null && rex.line > 0)
                {
                    Match token = Regex.Match(rex.Message, "found '([^']+)");
                    if (!token.Success)
                    {
                        token = Regex.Match(rex.Message, "\"([^\"]+)");
                    }
                    // find token position
                    if (token.Success)
                    {
                        string tok = token.Groups[1].Value;
                        int    p   = line.IndexOf(tok);
                        if (p > 0)
                        {
                            p = sci.MBSafeTextLength(line.Substring(0, p));
                            int len = sci.MBSafeTextLength(tok);
                            return(result + "characters " + p + "-" + (p + len) + " : " + rex.Message);
                        }
                    }
                    // fix column index
                    else
                    {
                        for (int i = 0; i < line.Length; i++)
                        {
                            if (line[i] == '\t')
                            {
                                col -= 7;
                            }
                            else if (line[i] != ' ')
                            {
                                break;
                            }
                        }
                    }
                }
                return(result + "character " + Math.Max(0, col) + " : " + rex.Message);
            }
            catch (TokenStreamRecognitionException trex)
            {
                int col = trex.recog.column;
                if (trex.recog.line > 0)
                {
                    // fix column index
                    string line = sci.GetLine(trex.recog.line - 1);
                    for (int i = 0; i < line.Length; i++)
                    {
                        if (line[i] == '\t')
                        {
                            col -= 7;
                        }
                        else if (line[i] != ' ')
                        {
                            break;
                        }
                    }
                }
                return(fileName + ":" + trex.recog.line + ": character " + col + " : " + trex.Message);
            }
            catch (TokenStreamException tex)
            {
                return(fileName + ": IO Error: " + tex.Message);
            }
            catch (Exception ex)
            {
                return(fileName + ": Validator Exception: " + ex.Message);
            }
            return(null);
        }