Esempio n. 1
0
        /// <summary>
        /// Looks for the best next position to insert new code, inserting new lines if needed
        /// </summary>
        /// <param name="startPos">The position inside the Scintilla document to start looking for the insertion position</param>
        /// <param name="endPos">The end position inside the Scintilla document</param>
        /// <param name="baseLine">The line inside the document to use as the base for the indentation level and detect if the desired point
        /// matches the end line</param>
        /// <param name="sci">The ScintillaControl where our document resides</param>
        /// <returns>The insertion point position</returns>
        private static int GetOrSetPointOfInsertion(int startPos, int endPos, int baseLine, ScintillaControl sci)
        {
            char[] characterClass = { ' ', '\r', '\n', '\t' };
            int nCount = 0;
            int extraLine = 1;

            int initialLn = sci.LineFromPosition(startPos);
            int baseIndent = sci.GetLineIndentation(baseLine);

            bool found = false;
            while (startPos <= endPos)
            {
                char c = (char)sci.CharAt(startPos);
                if (Array.IndexOf(characterClass, c) == -1)
                {
                    int endLn = sci.LineFromPosition(startPos);
                    if (endLn == baseLine || endLn == initialLn)
                    {
                        sci.InsertText(startPos, sci.NewLineMarker);
                        // Do we want to set the line indentation no matter what? {\r\t\t\t\r} -> {\r\t\r}
                        // Better results in most cases, but maybe highly unwanted in others?
                        sci.SetLineIndentation(++endLn, baseIndent + sci.Indent);
                        startPos = sci.LineIndentPosition(endLn);
                    }
                    if (c == '}')
                    {
                        sci.InsertText(startPos, sci.NewLineMarker);
                        sci.SetLineIndentation(endLn + 1, baseIndent);
                        // In relation with previous comment... we'll reinden this one: {\r} -> {\r\t\r}
                        if (sci.GetLineIndentation(endLn) <= baseIndent)
                        {
                            sci.SetLineIndentation(endLn, baseIndent + sci.Indent);
                            startPos = sci.LineIndentPosition(endLn);
                        }
                    }
                    found = true;
                    break;
                }
                else if (sci.EOLMode == 1 && c == '\r' && (++nCount) > extraLine)
                {
                    found = true;
                    break;
                }
                else if (c == '\n' && (++nCount) > extraLine)
                {
                    if (sci.EOLMode != 2)
                    {
                        startPos--;
                    }
                    found = true;
                    break;
                }
                startPos++;
            }

            if (!found) startPos--;

            return startPos;
        }
Esempio n. 2
0
        public static void GenerateExtractMethod(ScintillaControl Sci, string NewName)
        {
            FileModel cFile;
            IASContext context = ASContext.Context;

            string selection = Sci.SelText;
            if (string.IsNullOrEmpty(selection))
            {
                return;
            }

            if (selection.TrimStart().Length == 0)
            {
                return;
            }

            Sci.SetSel(Sci.SelectionStart + selection.Length - selection.TrimStart().Length,
                Sci.SelectionEnd);
            Sci.CurrentPos = Sci.SelectionEnd;

            int lineStart = Sci.LineFromPosition(Sci.SelectionStart);
            int lineEnd = Sci.LineFromPosition(Sci.SelectionEnd);
            int firstLineIndent = Sci.GetLineIndentation(lineStart);
            int entryPointIndent = Sci.Indent;

            for (int i = lineStart; i <= lineEnd; i++)
            {
                int indent = Sci.GetLineIndentation(i);
                if (i > lineStart)
                {
                    Sci.SetLineIndentation(i, indent - firstLineIndent + entryPointIndent);
                }
            }

            string selText = Sci.SelText;
            string template = TemplateUtils.GetTemplate("CallFunction");
            template = TemplateUtils.ReplaceTemplateVariable(template, "Name", NewName);
            template = TemplateUtils.ReplaceTemplateVariable(template, "Arguments", "");

            InsertCode(Sci.CurrentPos, template + ";", Sci);

            cFile = ASContext.Context.CurrentModel;
            ASFileParser parser = new ASFileParser();
            parser.ParseSrc(cFile, Sci.Text);

            FoundDeclaration found = GetDeclarationAtLine(Sci, lineStart);
            if (found == null || found.member == null)
            {
                return;
            }

            lookupPosition = Sci.CurrentPos;
            AddLookupPosition();

            MemberModel latest = TemplateUtils.GetTemplateBlockMember(Sci, TemplateUtils.GetBoundary("PrivateMethods"));

            if (latest == null)
                latest = GetLatestMemberForFunction(found.inClass, GetDefaultVisibility(found.inClass), found.member);

            if (latest == null)
                latest = found.member;

            int position = Sci.PositionFromLine(latest.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
            Sci.SetSel(position, position);

            FlagType flags = FlagType.Function;
            if ((found.member.Flags & FlagType.Static) > 0)
            {
                flags |= FlagType.Static;
            }

            MemberModel m = new MemberModel(NewName, context.Features.voidKey, flags, GetDefaultVisibility(found.inClass));

            template = NewLine + TemplateUtils.GetTemplate("Function");
            template = TemplateUtils.ToDeclarationWithModifiersString(m, template);
            template = TemplateUtils.ReplaceTemplateVariable(template, "Body", selText);
            template = TemplateUtils.ReplaceTemplateVariable(template, "BlankLine", NewLine);
            InsertCode(position, template, Sci);
        }
Esempio n. 3
0
 /// <summary>
 /// Checks if a line is in preprocessor block
 /// </summary>
 public bool LineIsInPreprocessor(ScintillaControl sci, int lexerPpStyle, int line)
 {
     bool ppEnd = false;
     bool ppStart = false;
     int foldHeader = (int)ScintillaNet.Enums.FoldLevel.HeaderFlag;
     for (var i = line; i > 0; i--)
     {
         int pos = sci.PositionFromLine(i);
         int ind = sci.GetLineIndentation(i);
         int style = sci.BaseStyleAt(pos + ind);
         if (style == lexerPpStyle)
         {
             int fold = sci.GetFoldLevel(i) & foldHeader;
             if (fold == foldHeader) ppStart = true;
             break;
         }
     }
     for (var i = line; i < sci.LineCount; i++)
     {
         int pos = sci.PositionFromLine(i);
         int ind = sci.GetLineIndentation(i);
         int style = sci.BaseStyleAt(pos + ind);
         if (style == lexerPpStyle)
         {
             int fold = sci.GetFoldLevel(i) & foldHeader;
             if (fold != foldHeader) ppEnd = true;
             break;
         }
     }
     if (ppStart && ppEnd) return true;
     else return false;
 }
Esempio n. 4
0
        private static void AssignStatementToVar(ClassModel inClass, ScintillaControl sci, MemberModel member)
        {
            int lineNum = sci.CurrentLine;
            string line = sci.GetLine(lineNum);
            StatementReturnType returnType = GetStatementReturnType(sci, inClass, line, sci.PositionFromLine(lineNum));

            if (returnType == null) return;
            
            string type = null;
            string varname = null;
            ASResult resolve = returnType.resolve;
            string word = returnType.word;

            if (resolve != null && !resolve.IsNull())
            {
                if (resolve.Member != null && resolve.Member.Type != null)
                {
                    type = resolve.Member.Type;
                }
                else if (resolve.Type != null && resolve.Type.Name != null)
                {
                    type = resolve.Type.QualifiedName;
                }

                if (resolve.Member != null && resolve.Member.Name != null)
                {
                    varname = GuessVarName(resolve.Member.Name, type);
                }
            }

            if (!string.IsNullOrEmpty(word) && Char.IsDigit(word[0])) word = null;

            if (!string.IsNullOrEmpty(word) && (string.IsNullOrEmpty(type) || Regex.IsMatch(type, "(<[^]]+>)")))
                word = null;

            if (!string.IsNullOrEmpty(type) && type.Equals("void", StringComparison.OrdinalIgnoreCase))
                type = null;

            if (varname == null) varname = GuessVarName(word, type);

            if (varname != null && varname == word)
                varname = varname.Length == 1 ? varname + "1" : varname[0] + "";

            string cleanType = null;
            if (type != null) cleanType = FormatType(GetShortType(type));
            
            string template = TemplateUtils.GetTemplate("AssignVariable");
            template = TemplateUtils.ReplaceTemplateVariable(template, "Name", varname);
            template = TemplateUtils.ReplaceTemplateVariable(template, "Type", cleanType);

            int indent = sci.GetLineIndentation(lineNum);
            int pos = sci.PositionFromLine(lineNum) + indent / sci.Indent;

            sci.CurrentPos = pos;
            sci.SetSel(pos, pos);
            InsertCode(pos, template, sci);

            if (type != null)
            {
                ClassModel inClassForImport = null;
                if (resolve.InClass != null)
                {
                    inClassForImport = resolve.InClass;
                }
                else if (resolve.RelClass != null)
                {
                    inClassForImport = resolve.RelClass;
                }
                else 
                {
                    inClassForImport = inClass;
                }
                List<string> l = new List<string>();
                l.Add(GetQualifiedType(type, inClassForImport));
                pos += AddImportsByName(l, sci.LineFromPosition(pos));
            }
        }
        /// <summary>
        /// Add closing brace to a code block.
        /// If enabled, move the starting brace to a new line.
        /// </summary>
        /// <param name="Sci"></param>
        /// <param name="txt"></param>
        /// <param name="line"></param>
        public static void AutoCloseBrace(ScintillaControl Sci, int line)
        {
            // find matching brace
            int bracePos = Sci.LineEndPosition(line - 1) - 1;
            while ((bracePos > 0) && (Sci.CharAt(bracePos) != '{')) bracePos--;
            if (bracePos == 0 || Sci.BaseStyleAt(bracePos) != 5) return;
            int match = Sci.SafeBraceMatch(bracePos);
            int start = line;
            int indent = Sci.GetLineIndentation(start - 1);
            if (match > 0)
            {
                int endIndent = Sci.GetLineIndentation(Sci.LineFromPosition(match));
                if (endIndent + Sci.TabWidth > indent)
                    return;
            }

            // find where to include the closing brace
            int startIndent = indent;
            int count = Sci.LineCount;
            int lastLine = line;
            int position;
            string txt = Sci.GetLine(line).Trim();
            line++;
            int eolMode = Sci.EOLMode;
            string NL = LineEndDetector.GetNewLineMarker(eolMode);

            if (txt.Length > 0 && ")]};,".IndexOf(txt[0]) >= 0)
            {
                Sci.BeginUndoAction();
                try
                {
                    position = Sci.CurrentPos;
                    Sci.InsertText(position, NL + "}");
                    Sci.SetLineIndentation(line, startIndent);
                }
                finally
                {
                    Sci.EndUndoAction();
                }
                return;
            }
            else
            {
                while (line < count - 1)
                {
                    txt = Sci.GetLine(line).TrimEnd();
                    if (txt.Length != 0)
                    {
                        indent = Sci.GetLineIndentation(line);
                        if (indent <= startIndent) break;
                        lastLine = line;
                    }
                    else break;
                    line++;
                }
            }
            if (line >= count - 1) lastLine = start;

            // insert closing brace
            Sci.BeginUndoAction();
            try
            {
                position = Sci.LineEndPosition(lastLine);
                Sci.InsertText(position, NL + "}");
                Sci.SetLineIndentation(lastLine + 1, startIndent);
            }
            finally
            {
                Sci.EndUndoAction();
            }
        }
        /// <summary>
        /// Handles the incoming character
        /// </summary> 
        public static void OnChar(ScintillaControl sci, Int32 value)
        {
            if (cType == XMLType.Invalid || (sci.ConfigurationLanguage != "xml" && sci.ConfigurationLanguage != "html")) 
                return;
            XMLContextTag ctag;
            Int32 position = sci.CurrentPos;
            if (sci.BaseStyleAt(position) == 6 && value != '"')
                return; // in XML attribute

            Char c = ' ';
            DataEvent de;
            switch (value)
            {
                case 10:
                    // Shift+Enter to insert <BR/>
                    Int32 line = sci.LineFromPosition(position);
                    if (Control.ModifierKeys == Keys.Shift)
                    {
                        ctag = GetXMLContextTag(sci, position);
                        if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
                        {
                            int start = sci.PositionFromLine(line)-((sci.EOLMode == 0)? 2:1);
                            sci.SetSel(start, position);
                            sci.ReplaceSel((PluginSettings.UpperCaseHtmlTags) ? "<BR/>" : "<br/>");
                            sci.SetSel(start+5, start+5);
                            return;
                        }
                    }
                    if (PluginSettings.SmartIndenter)
                    {
                        // There is no standard for XML formatting, although most IDEs have similarities. We are mostly going with Visual Studio style with slight differences.
                        // Get last non-empty line.
                        String text = "";
                        Int32 line2 = line - 1;
                        while (line2 >= 0 && text.Length == 0)
                        {
                            text = sci.GetLine(line2).TrimEnd();
                            line2--;
                        }
                        if ((text.EndsWith(">") && !text.EndsWith("?>") && !text.EndsWith("%>")) || text.EndsWith("<!--") || text.EndsWith("<![CDATA["))
                        {
                            // Get the previous tag.
                            do
                            {
                                position--;
                                c = (Char)sci.CharAt(position);
                            }
                            while (position > 0 && c != '>');
                            ctag = GetXMLContextTag(sci, c == '>' ? position + 1 : position);
                            // Line indentation.
                            Int32 indent = sci.GetLineIndentation(line2 + 1);

                            String checkStart = null;
                            bool subIndent = true;
                            if (text.EndsWith("<!--")) { checkStart = "-->"; subIndent = false; }
                            else if (text.EndsWith("<![CDATA[")) { checkStart = "]]>"; subIndent = false; }
                            else if (ctag.Closed || ctag.Closing)
                            {
                                //Closed tag. Look for the nearest open and not closed tag for proper indentation
                                subIndent = false;
                                if (ctag.Name != null)
                                {
                                    var tmpTags = new Stack<XMLContextTag>();
                                    var tmpTag = ctag;

                                    if (!tmpTag.Closed) tmpTags.Push(tmpTag);
                                    while (tmpTag.Position != 0)
                                    {
                                        tmpTag = GetXMLContextTag(sci, tmpTag.Position);
                                        if (tmpTag.Tag != null && tmpTag.Name != null)
                                        {
                                            if (tmpTag.Closed) 
                                                continue;
                                            else if (tmpTag.Closing)
                                            {
                                                tmpTags.Push(tmpTag);
                                            }
                                            else
                                            {
                                                if (tmpTags.Count > 0 && tmpTags.Peek().Name == tmpTag.Name)
                                                    tmpTags.Pop();
                                                else
                                                    break;
                                            }
                                        }
                                    }
                                    if (tmpTags.Count > 0)
                                        indent = sci.GetLineIndentation(sci.LineFromPosition(tmpTags.Pop().Position));
                                    else if (tmpTag.Name != null)
                                    {
                                        subIndent = true;
                                        checkStart = "</" + tmpTag.Name;
                                        indent = sci.GetLineIndentation(sci.LineFromPosition(tmpTag.Position));
                                    }
                                    else
                                    {
                                        indent = sci.GetLineIndentation(sci.LineFromPosition(tmpTag.Position));
                                    }
                                }
                            }
                            else if (ctag.Name != null)
                            {
                                // Indentation. Some IDEs use the tag position, VS uses the tag start line indentation. 
                                indent = sci.GetLineIndentation(sci.LineFromPosition(ctag.Position));
                                checkStart = "</" + ctag.Name;
                                if (ctag.Name.ToLower() == "script" || ctag.Name.ToLower() == "style") 
                                    subIndent = false;
                            }
                            try
                            {
                                sci.BeginUndoAction();
                                if (checkStart != null)
                                {
                                    text = sci.GetLine(line).TrimStart();
                                    if (text.StartsWith(checkStart))
                                    {
                                        sci.SetLineIndentation(line, indent);
                                        sci.InsertText(sci.PositionFromLine(line), LineEndDetector.GetNewLineMarker(sci.EOLMode));
                                    }
                                }
                                // Indent the code
                                if (subIndent) indent += sci.Indent;
                                sci.SetLineIndentation(line, indent);
                                position = sci.LineIndentPosition(line);
                                sci.SetSel(position, position);
                            }
                            finally { sci.EndUndoAction(); }
                            return;
                        }
                        else if (!text.EndsWith(">"))
                        {
                            ctag = GetXMLContextTag(sci, sci.CurrentPos);
                            if (ctag.Tag == null || ctag.Name == null) return;
                            // We're inside a tag. Visual Studio indents with regards to the first line, other IDEs indent using the indentation of the last line with text.
                            int indent;
                            string tag = (ctag.Tag.IndexOf('\r') > 0 || ctag.Tag.IndexOf('\n') > 0) ? ctag.Tag.Substring(0, ctag.Tag.IndexOfAny(new[] {'\r', '\n'})).TrimEnd() : ctag.Tag.TrimEnd();
                            if (tag.EndsWith("\""))
                            {
                                int i;
                                int l = tag.Length;
                                for (i = ctag.Name.Length + 1; i < l; i++)
                                {
                                    if (!char.IsWhiteSpace(tag[i]))
                                        break;
                                }
                                indent = sci.Column(ctag.Position) + sci.MBSafePosition(i);
                            }
                            else
                            {
                                indent = sci.GetLineIndentation(sci.LineFromPosition(ctag.Position)) + sci.Indent;
                            }

                            sci.SetLineIndentation(line, indent);
                            position = sci.LineIndentPosition(line);
                            sci.SetSel(position, position);
                            return;
                        }
                    }
                    break;
                    
                case '<':
                case '/':
                    if (value == '/')
                    {
                        if ((position < 2) || ((Char)sci.CharAt(position-2) != '<')) return;
                        ctag = new XMLContextTag();
                        ctag.Position = position - 2;
                        ctag.Closing = true;
                    }
                    else 
                    {
                        ctag = GetXMLContextTag(sci, position);
                        if (ctag.Tag != null) return;
                    }
                    // Allow another plugin to handle this
                    de = new DataEvent(EventType.Command, "XMLCompletion.Element", ctag);
                    EventManager.DispatchEvent(PluginBase.MainForm, de);
                    if (de.Handled) return;

                    // New tag
                    if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known)
                    {
                        List<ICompletionListItem> items = new List<ICompletionListItem>();
                        String previous = null;
                        foreach (string ns in namespaces)
                        {
                            items.Add(new NamespaceItem(ns));
                        }
                        foreach (HTMLTag tag in knownTags) 
                            if (tag.Name != previous && (tag.NS == "" || tag.NS == defaultNS)) 
                            {
                                items.Add( new HtmlTagItem(tag.Name, tag.Tag));
                                previous = tag.Name;
                            }
                        items.Sort(new ListItemComparer());
                        CompletionList.Show(items, true);
                    }
                    return;

                case ':':
                    ctag = GetXMLContextTag(sci, position);
                    if (ctag.NameSpace == null || position - ctag.Position > ctag.Name.Length + 2) return;
                    // Allow another plugin to handle this
                    de = new DataEvent(EventType.Command, "XMLCompletion.Namespace", ctag);
                    EventManager.DispatchEvent(PluginBase.MainForm, de);
                    if (de.Handled) return;

                    // Show namespace's tags
                    if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known)
                    {
                        List<ICompletionListItem> items = new List<ICompletionListItem>();
                        String previous = null;
                        foreach (HTMLTag tag in knownTags)
                            if (tag.Name != previous && tag.NS == ctag.NameSpace)
                            {
                                items.Add(new HtmlTagItem(tag.Name, tag.Name));
                                previous = tag.Name;
                            }
                        CompletionList.Show(items, true);
                    }
                    return;

                case '>':
                    if (PluginSettings.CloseTags)
                    {
                        ctag = GetXMLContextTag(sci, position);
                        if (ctag.Name != null && !ctag.Closed)
                        {
                            // Allow another plugin to handle this
                            de = new DataEvent(EventType.Command, "XMLCompletion.CloseElement", ctag);
                            EventManager.DispatchEvent(PluginBase.MainForm, de);
                            if (de.Handled) return;

                            if (ctag.Closing) return;

                            Boolean isLeaf = false;
                            if (cType == XMLType.Known)
                            foreach(HTMLTag tag in knownTags)
                            {
                                if (String.Compare(tag.Tag, ctag.Name, true) == 0)
                                {
                                    isLeaf = tag.IsLeaf;
                                    break;
                                }
                            }
                            if (isLeaf)
                            {
                                sci.SetSel(position-1,position);
                                sci.ReplaceSel("/>");
                                sci.SetSel(position+1, position+1);
                            }
                            else
                            {
                                String closeTag = "</"+ctag.Name+">";
                                sci.ReplaceSel(closeTag);
                                sci.SetSel(position, position);
                            }
                        }
                    }
                    return;
                    
                case ' ':
                    c = (char)sci.CharAt(position);
                    if (c > 32 && c != '/' && c != '>' && c != '<') return;
                    ctag = GetXMLContextTag(sci, position);
                    if (ctag.Tag != null)
                    {
                        if (InQuotes(ctag.Tag) || ctag.Tag.LastIndexOf('"') < ctag.Tag.LastIndexOf('=')) return;
                        // Allow another plugin to handle this
                        Object[] obj = new Object[] { ctag, "" };
                        de = new DataEvent(EventType.Command, "XMLCompletion.Attribute", obj);
                        EventManager.DispatchEvent(PluginBase.MainForm, de);
                        if (de.Handled) return;
                        
                        if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known)
                        {
                            foreach (HTMLTag tag in knownTags)
                                if (String.Compare(tag.Tag, ctag.Name, true) == 0)
                                {
                                    List<ICompletionListItem> items = new List<ICompletionListItem>();
                                    String previous = null;
                                    foreach (String attr in tag.Attributes)
                                        if (attr != previous)
                                        {
                                            items.Add(new HtmlAttributeItem(attr));
                                            previous = attr;
                                        }
                                    CompletionList.Show(items, true);
                                    return;
                                }
                        }
                    }
                    /*else
                    {
                        if (Control.ModifierKeys == Keys.Shift)
                        {
                            sci.SetSel(position - 1, position);
                            sci.ReplaceSel("&nbsp;");
                        }
                    }*/
                    return;
                
                case '=':
                    if (PluginSettings.InsertQuotes)
                    {
                        ctag = GetXMLContextTag(sci, position);
                        position = sci.CurrentPos-2;
                        if (ctag.Tag != null && !String.IsNullOrEmpty(ctag.Name) && Char.IsLetter(ctag.Name[0]) 
                            && !InQuotes(ctag.Tag) && (GetWordLeft(sci, ref position).Length > 0))
                        {
                            position = sci.CurrentPos;
                            c = (Char)sci.CharAt(position);
                            if (c > 32 && c != '>') sci.ReplaceSel("\"\" ");
                            else sci.ReplaceSel("\"\"");
                            sci.SetSel(position+1, position+1);
                            justInsertedQuotesAt = position+1;
                            // Allow another plugin to handle this
                            de = new DataEvent(EventType.Command, "XMLCompletion.AttributeValue", new Object[] { ctag, string.Empty });
                            EventManager.DispatchEvent(PluginBase.MainForm, de);
                        }
                    }
                    return;

                case '"':
                    ctag = GetXMLContextTag(sci, position);
                    if (position > 1 && ctag.Tag != null && !ctag.Tag.StartsWith("<!"))
                    {
                        // TODO  Colorize text change to highlight what's been done
                        if (justInsertedQuotesAt == position - 1)
                        {
                            justInsertedQuotesAt = -1;
                            c = (Char)sci.CharAt(position - 2);
                            if (c == '"' && (Char)sci.CharAt(position-2) == '"')
                            {
                                sci.SetSel(position - 2, position);
                                sci.ReplaceSel("\"");
                            }
                            // Allow another plugin to handle this
                            de = new DataEvent(EventType.Command, "XMLCompletion.AttributeValue", new Object[] {ctag, string.Empty});
                            EventManager.DispatchEvent(PluginBase.MainForm, de);
                        }
                        else
                        {
                            c = (Char)sci.CharAt(position - 1);
                            if (c == '"' && (Char)sci.CharAt(position) == '"')
                            {
                                sci.SetSel(position - 1, position + 1);
                                sci.ReplaceSel("\"");
                            }
                        }
                    }
                    break;
                    
                case '?':
                case '%':
                    if (PluginSettings.CloseTags && position > 1)
                    {
                        ctag = GetXMLContextTag(sci, position-2);
                        if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
                        {
                            if ((Char)sci.CharAt(position-2) == '<')
                            {
                                sci.ReplaceSel((Char)value + ">");
                                sci.SetSel(position, position);
                            }
                        }
                    }
                    break;
                
                case '!':
                    if (PluginSettings.CloseTags && position > 1)
                    {
                        ctag = GetXMLContextTag(sci, position-2);
                        if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
                        {
                            if ((Char)sci.CharAt(position-2) == '<')
                            {
                                CompletionList.Show(xmlBlocks, true);
                            }
                        }                       
                    }
                    break;
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Handles the incoming character
        /// </summary> 
		public static void OnChar(ScintillaControl sci, Int32 value)
		{
            if (cType == XMLType.Invalid || (sci.ConfigurationLanguage != "xml" && sci.ConfigurationLanguage != "html")) 
                return;
			XMLContextTag ctag;
			Int32 position = sci.CurrentPos;
            if (sci.BaseStyleAt(position) == 6 && value != '"')
                return; // in XML attribute

			Char c = ' ';
            DataEvent de;
			switch (value)
			{
				case 10:
                    // Shift+Enter to insert <BR/>
                    Int32 line = sci.LineFromPosition(position);
					if (Control.ModifierKeys == Keys.Shift)
					{
						ctag = GetXMLContextTag(sci, position);
						if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
						{
							int start = sci.PositionFromLine(line)-((sci.EOLMode == 0)? 2:1);
							sci.SetSel(start, position);
                            sci.ReplaceSel((PluginSettings.UpperCaseHtmlTags) ? "<BR/>" : "<br/>");
							sci.SetSel(start+5, start+5);
							return;
						}
					}
                    if (PluginSettings.SmartIndenter)
					{
                        // Get last non-empty line
						String text = "";
                        Int32 line2 = line - 1;
						while (line2 >= 0 && text.Length == 0)
						{
							text = sci.GetLine(line2).TrimEnd();
							line2--;
						}
						if ((text.EndsWith(">") && !text.EndsWith("?>") && !text.EndsWith("%>") && !closingTag.IsMatch(text)) || text.EndsWith("<!--") || text.EndsWith("<![CDATA["))
						{
                            // Get the previous tag
                            do
                            {
								position--;
								c = (Char)sci.CharAt(position);
							}
							while (position > 0 && c != '>');
							ctag = GetXMLContextTag(sci, c == '>' ? position + 1 : position);
							if ((Char)sci.CharAt(position-1) == '/') return;
                            // Insert blank line if we pressed Enter between a tag & it's closing tag
                            Int32 indent = sci.GetLineIndentation(line2 + 1);
							String checkStart = null;
                            bool subIndent = true;
							if (text.EndsWith("<!--")) { checkStart = "-->"; subIndent = false; }
                            else if (text.EndsWith("<![CDATA[")) { checkStart = "]]>"; subIndent = false; }
                            else if (ctag.Closed) subIndent = false;
                            else if (ctag.Name != null)
                            {
                                checkStart = "</" + ctag.Name;
                                if (ctag.Name.ToLower() == "script" || ctag.Name.ToLower() == "style") 
                                    subIndent = false;
                                if (ctag.Tag.IndexOf('\r') > 0 || ctag.Tag.IndexOf('\n') > 0)
                                    subIndent = false;
                            }
							if (checkStart != null)
							{
								text = sci.GetLine(line).TrimStart();
								if (text.StartsWith(checkStart))
								{
									sci.SetLineIndentation(line, indent);
									sci.InsertText(sci.PositionFromLine(line), LineEndDetector.GetNewLineMarker(sci.EOLMode));
								}
							}
                            // Indent the code
                            if (subIndent) indent += sci.Indent;
                            sci.SetLineIndentation(line, indent);
							position = sci.LineIndentPosition(line);
							sci.SetSel(position, position);
							return;
						}
					}
					break;
					
				case '<':
				case '/':
					if (value == '/')
					{
						if ((position < 2) || ((Char)sci.CharAt(position-2) != '<')) return;
                        ctag = new XMLContextTag();
                        ctag.Closing = true;
					}
					else 
					{
						ctag = GetXMLContextTag(sci, position);
						if (ctag.Tag != null) return;
					}
                    // Allow another plugin to handle this
                    de = new DataEvent(EventType.Command, "XMLCompletion.Element", ctag);
                    EventManager.DispatchEvent(PluginBase.MainForm, de);
                    if (de.Handled) return;

                    // New tag
                    if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known)
					{
                        List<ICompletionListItem> items = new List<ICompletionListItem>();
						String previous = null;
                        foreach (string ns in namespaces)
                        {
                            items.Add(new NamespaceItem(ns));
                        }
                        foreach (HTMLTag tag in knownTags) 
						    if (tag.Name != previous && (tag.NS == "" || tag.NS == defaultNS)) 
                            {
							    items.Add( new HtmlTagItem(tag.Name, tag.Tag));
							    previous = tag.Name;
						    }
                        items.Sort(new ListItemComparer());
                        CompletionList.Show(items, true);
					}
					return;

                case ':':
                    ctag = GetXMLContextTag(sci, position);
                    if (ctag.NameSpace == null || position - ctag.Position > ctag.Name.Length + 2) return;
                    // Allow another plugin to handle this
                    de = new DataEvent(EventType.Command, "XMLCompletion.Namespace", ctag);
                    EventManager.DispatchEvent(PluginBase.MainForm, de);
                    if (de.Handled) return;

                    // Show namespace's tags
                    if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known)
                    {
                        List<ICompletionListItem> items = new List<ICompletionListItem>();
                        String previous = null;
                        foreach (HTMLTag tag in knownTags)
                            if (tag.Name != previous && tag.NS == ctag.NameSpace)
                            {
                                items.Add(new HtmlTagItem(tag.Name, tag.Name));
                                previous = tag.Name;
                            }
                        CompletionList.Show(items, true);
                    }
                    return;

				case '>':
                    if (PluginSettings.CloseTags)
					{
						ctag = GetXMLContextTag(sci, position);
						if (ctag.Name != null && !ctag.Closed)
						{
                            // Allow another plugin to handle this
                            de = new DataEvent(EventType.Command, "XMLCompletion.CloseElement", ctag);
                            EventManager.DispatchEvent(PluginBase.MainForm, de);
                            if (de.Handled) return;

                            if (ctag.Closing) return;

							Boolean isLeaf = false;
							if (cType == XMLType.Known)
							foreach(HTMLTag tag in knownTags)
							{
								if (String.Compare(tag.Tag, ctag.Name, true) == 0)
								{
									isLeaf = tag.IsLeaf;
									break;
								}
							}
							if (isLeaf)
							{
								sci.SetSel(position-1,position);
								sci.ReplaceSel("/>");
								sci.SetSel(position+1, position+1);
							}
							else
							{
								String closeTag = "</"+ctag.Name+">";
								sci.ReplaceSel(closeTag);
								sci.SetSel(position, position);
							}
						}
					}
					return;
					
				case ' ':
					c = (char)sci.CharAt(position);
					if (c > 32 && c != '/' && c != '>' && c != '<') return;
					ctag = GetXMLContextTag(sci, position);
                    if (ctag.Tag != null)
                    {
                        if (InQuotes(ctag.Tag) || ctag.Tag.LastIndexOf('"') < ctag.Tag.LastIndexOf('=')) return;
                        // Allow another plugin to handle this
                        Object[] obj = new Object[] { ctag, "" };
                        de = new DataEvent(EventType.Command, "XMLCompletion.Attribute", obj);
                        EventManager.DispatchEvent(PluginBase.MainForm, de);
                        if (de.Handled) return;
                        
                        if (PluginSettings.EnableXMLCompletion && cType == XMLType.Known)
                        {
                            foreach (HTMLTag tag in knownTags)
                                if (String.Compare(tag.Tag, ctag.Name, true) == 0)
                                {
                                    List<ICompletionListItem> items = new List<ICompletionListItem>();
                                    String previous = null;
                                    foreach (String attr in tag.Attributes)
                                        if (attr != previous)
                                        {
                                            items.Add(new HtmlAttributeItem(attr));
                                            previous = attr;
                                        }
                                    CompletionList.Show(items, true);
                                    return;
                                }
                        }
                    }
                    /*else
                    {
                        if (Control.ModifierKeys == Keys.Shift)
                        {
                            sci.SetSel(position - 1, position);
                            sci.ReplaceSel("&nbsp;");
                        }
                    }*/
					return;
				
				case '=':
					if (PluginSettings.InsertQuotes)
					{
						ctag = GetXMLContextTag(sci, position);
						position = sci.CurrentPos-2;
						if (ctag.Tag != null && !ctag.Tag.StartsWith("<!") && !InQuotes(ctag.Tag) && (GetWordLeft(sci, ref position).Length > 0))
						{
							position = sci.CurrentPos;
							c = (Char)sci.CharAt(position);
							if (c > 32 && c != '>') sci.ReplaceSel("\"\" ");
							else sci.ReplaceSel("\"\"");
							sci.SetSel(position+1, position+1);
                            justInsertedQuotesAt = position+1;
                            // Allow another plugin to handle this
                            de = new DataEvent(EventType.Command, "XMLCompletion.AttributeValue", new XMLContextTag());
                            EventManager.DispatchEvent(PluginBase.MainForm, de);
						}
					}
					return;

                case '"':
                    ctag = GetXMLContextTag(sci, position);
                    if (position > 1 && ctag.Tag != null && !ctag.Tag.StartsWith("<!"))
                    {
                        // TODO  Colorize text change to highlight what's been done
                        if (justInsertedQuotesAt == position - 1)
                        {
                            justInsertedQuotesAt = -1;
                            c = (Char)sci.CharAt(position - 2);
                            if (c == '"' && (Char)sci.CharAt(position-2) == '"')
                            {
                                sci.SetSel(position - 2, position);
                                sci.ReplaceSel("\"");
                            }
                            // Allow another plugin to handle this
                            de = new DataEvent(EventType.Command, "XMLCompletion.AttributeValue", new XMLContextTag());
                            EventManager.DispatchEvent(PluginBase.MainForm, de);
                        }
                        else
                        {
                            c = (Char)sci.CharAt(position - 1);
                            if (c == '"' && (Char)sci.CharAt(position) == '"')
                            {
                                sci.SetSel(position - 1, position + 1);
                                sci.ReplaceSel("\"");
                            }
                        }
                    }
                    break;
					
				case '?':
				case '%':
					if (PluginSettings.CloseTags && position > 1)
					{
						ctag = GetXMLContextTag(sci, position-2);
						if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
						{
							if ((Char)sci.CharAt(position-2) == '<')
							{
								sci.ReplaceSel((Char)value + ">");
								sci.SetSel(position, position);
							}
						}
					}
					break;
				
				case '!':
                    if (PluginSettings.CloseTags && position > 1)
					{
						ctag = GetXMLContextTag(sci, position-2);
						if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
						{
							if ((Char)sci.CharAt(position-2) == '<')
							{
								CompletionList.Show(xmlBlocks, true);
							}
						}						
					}
					break;
			}
		}
Esempio n. 8
0
 /// <summary>
 /// Inserts the specified snippet to the document
 /// </summary>
 public static Int32 InsertSnippetText(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();
     }
 }
        public void Execute()
        {
            Sci = PluginBase.MainForm.CurrentDocument.SciControl;
            Sci.BeginUndoAction();
            try
            {
                IASContext context = ASContext.Context;

                string selection = Sci.SelText;
                if (selection == null || selection.Length == 0)
                {
                    return;
                }

                if (selection.TrimStart().Length == 0)
                {
                    return;
                }

                Sci.SetSel(Sci.SelectionStart + selection.Length - selection.TrimStart().Length,
                    Sci.SelectionEnd);
                Sci.CurrentPos = Sci.SelectionEnd;

                Int32 pos = Sci.CurrentPos;

                int lineStart = Sci.LineFromPosition(Sci.SelectionStart);
                int lineEnd = Sci.LineFromPosition(Sci.SelectionEnd);
                int firstLineIndent = Sci.GetLineIndentation(lineStart);
                int entryPointIndent = Sci.Indent;

                for (int i = lineStart; i <= lineEnd; i++)
                {
                    int indent = Sci.GetLineIndentation(i);
                    if (i > lineStart)
                    {
                        Sci.SetLineIndentation(i, indent - firstLineIndent + entryPointIndent);
                    }
                }

                string selText = Sci.SelText;
                Sci.ReplaceSel(NewName + "();");

                cFile = ASContext.Context.CurrentModel;
                ASFileParser parser = new ASFileParser();
                parser.ParseSrc(cFile, Sci.Text);

                bool isAs3 = cFile.Context.Settings.LanguageId == "AS3";

                FoundDeclaration found = GetDeclarationAtLine(Sci, lineStart);
                if (found == null || found.member == null)
                {
                    return;
                }

                int position = Sci.PositionFromLine(found.member.LineTo + 1) - ((Sci.EOLMode == 0) ? 2 : 1);
                Sci.SetSel(position, position);

                StringBuilder sb = new StringBuilder();
                sb.Append("$(Boundary)\n\n");
                if ((found.member.Flags & FlagType.Static) > 0)
                {
                    sb.Append("static ");
                }
                sb.Append(ASGenerator.GetPrivateKeyword());
                sb.Append(" function ");
                sb.Append(NewName);
                sb.Append("():");
                sb.Append(isAs3 ? "void " : "Void ");
                sb.Append("$(CSLB){\n\t");
                sb.Append(selText);
                sb.Append("$(EntryPoint)");
                sb.Append("\n}\n$(Boundary)");

                ASGenerator.InsertCode(position, sb.ToString());
            }
            finally
            {
                Sci.EndUndoAction();
            }
        }
Esempio n. 10
0
		static public void OnChar(ScintillaControl sci, int value)
		{
			if (cType == XMLType.Invalid)
				return;
			
			XMLContextTag ctag;
			int position = sci.CurrentPos;
			char c = ' ';
			switch (value)
			{
				case 10:
					int line = sci.LineFromPosition(position);
					
					// Shift+Enter to insert <BR/>
					if (Control.ModifierKeys == Keys.Shift)
					{
						ctag = GetXMLContextTag(sci, position);
						if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
						{
							int start = sci.PositionFromLine(line)-((sci.EOLMode == 0)? 2:1);
							sci.SetSel(start, position);
							sci.ReplaceSel((lowerCaseHtmlTags)?"<br/>":"<BR/>");
							sci.SetSel(start+5, start+5);
							return;
						}
					}
					if (autoIndent)
					{
						// get last non-empty line
						string text = "";
						int line2 = line-1;
						while (line2 >= 0 && text.Length == 0)
						{
							text = sci.GetLine(line2).TrimEnd();
							line2--;
						}
						
						if ((text.EndsWith(">") && !text.EndsWith("?>") && !text.EndsWith("%>") && !re_closingTag.IsMatch(text)) 
						    || text.EndsWith("<!--") || text.EndsWith("<![CDATA["))
						{
							// get the previous tag
							do {
								position--;
								c = (char)sci.CharAt(position);
							}
							while (position > 0 && c != '>');
							ctag = GetXMLContextTag(sci, position);
							
							if ((char)sci.CharAt(position-1) == '/')
								return;
							
							// insert blank line if we pressed Enter between a tag & it's closing tag
							int indent = sci.GetLineIndentation(line2+1);
							string checkStart = null;
							if (text.EndsWith("<!--")) checkStart = "-->";
							else if (text.EndsWith("<![CDATA[")) checkStart = "]]>";
							else if (ctag.Name != null) checkStart = "</"+ctag.Name;
							if (checkStart != null)
							{
								text = sci.GetLine(line).TrimStart();
								if (text.StartsWith(checkStart))
								{
									sci.SetLineIndentation(line, indent);
									sci.InsertText(sci.PositionFromLine(line), mainForm.GetNewLineMarker(sci.EOLMode));
								}
							}
							
							// indent
							sci.SetLineIndentation(line, indent+sci.Indent);
							position = sci.LineIndentPosition(line);
							sci.SetSel(position, position);
							return;
						}
					}
					break;
					
				case '<':
				case '/':
					if (value == '/')
					{
						if ((position < 2) || ((char)sci.CharAt(position-2) != '<'))
							return;
					}
					else 
					{
						ctag = GetXMLContextTag(sci, position);
						if (ctag.Tag != null)
							return;
					}
					// new tag
					if (enableHtmlCompletion && cType == XMLType.Known)
					{
						ArrayList items = new ArrayList();
						string previous = null;
						foreach(HTMLTag tag in knownTags) 
						if (tag.Name != previous) {
							items.Add( new HtmlTagItem(tag.Name, tag.Tag) );
							previous = tag.Name;
						}
						CompletionList.Show(items, true);
					}
					else
					{
						// allow another plugin to handle this
						mainForm.DispatchEvent(new DataEvent(EventType.CustomData,"XMLCompletion.Element",new XMLContextTag()));
					}
					return;
					
				case '>':
					if (autoCloseTags)
					{
						ctag = GetXMLContextTag(sci, position);
						if (ctag.Name != null && !ctag.Tag.EndsWith("/>"))
						{
							// est-ce un tag sans enfant?
							bool isLeaf = false;
							if (cType == XMLType.Known)
							foreach(HTMLTag tag in knownTags)
							{
								if (String.Compare(tag.Tag, ctag.Name, true) == 0)
								{
									isLeaf = tag.IsLeaf;
									break;
								}
							}
							if (isLeaf)
							{
								sci.SetSel(position-1,position);
								sci.ReplaceSel("/>");
								sci.SetSel(position+1, position+1);
							}
							else
							{
								string closeTag = "</"+ctag.Name+">";
								sci.ReplaceSel(closeTag);
								sci.SetSel(position, position);
							}
						}
					}
					return;
					
				case ' ':
					c = (char)sci.CharAt(position);
					if (c > 32 && c != '/' && c != '>' && c != '<')
						return;
						
					ctag = GetXMLContextTag(sci, position);
					if (ctag.Tag != null)
					{
						if (InQuotes(ctag.Tag) || ctag.Tag.LastIndexOf('"') < ctag.Tag.LastIndexOf('='))
							return;
						//
						if (enableHtmlCompletion && cType == XMLType.Known)
						{
							foreach(HTMLTag tag in knownTags)
							if (String.Compare(tag.Tag, ctag.Name, true) == 0)
							{
								ArrayList items = new ArrayList();
								string previous = null;
								foreach(string attr in tag.Attributes)
								if (attr != previous) {
									items.Add( new HtmlAttributeItem(attr) );
									previous = attr;
								}
								CompletionList.Show(items, true);
								return;
							}
						}
						else
						{
							// allow another plugin to handle this
							object[] o = new object[]{ctag,""};
							mainForm.DispatchEvent(new DataEvent(EventType.CustomData,"XMLCompletion.Attribute",o));
						}
					}
					return;
				
				case '=':
					if (insertQuotes)
					{
						ctag = GetXMLContextTag(sci, position);
						position = sci.CurrentPos-2;
						if (ctag.Tag != null && !InQuotes(ctag.Tag) && (GetWordLeft(sci, ref position).Length > 0))
						{
							position = sci.CurrentPos;
							c = (char)sci.CharAt(position);
							if (c > 32 && c != '>') sci.ReplaceSel("\"\" ");
							else sci.ReplaceSel("\"\"");
							sci.SetSel(position+1, position+1);
						}
					}
					return;
					
				case '?':
				case '%':
					if (autoCloseTags && position > 1)
					{
						ctag = GetXMLContextTag(sci, position-2);
						if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
						{
							if ((char)sci.CharAt(position-2) == '<')
							{
								sci.ReplaceSel((char)value + ">");
								sci.SetSel(position, position);
							}
						}
					}
					break;
				
				case '!':
					if (autoCloseTags && position > 1)
					{
						ctag = GetXMLContextTag(sci, position-2);
						if (ctag.Tag == null || ctag.Tag.EndsWith(">"))
						{
							if ((char)sci.CharAt(position-2) == '<')
							{
								CompletionList.Show(xmlBlocks, true);
							}
						}						
					}
					break;
			}
		}
        public static int GetBodyStart(int lineFrom, int lineTo, ScintillaControl Sci, int pos)
        {
            int posStart = Sci.PositionFromLine(lineFrom);
            int posEnd = Sci.LineEndPosition(lineTo);

            Sci.SetSel(posStart, posEnd);

            List<char> characterClass = new List<char>(new char[] { ' ', '\r', '\n', '\t' });
            string currentMethodBody = Sci.SelText;
            int nCount = 0;
            int funcBodyStart = pos;
            int extraLine = 0;
            if (pos == -1)
            {
                funcBodyStart = posStart + currentMethodBody.IndexOf('{');
                extraLine = 1;
            }
            while (funcBodyStart <= posEnd)
            {
                char c = (char)Sci.CharAt(++funcBodyStart);
                if (c == '}')
                {
                    int ln = Sci.LineFromPosition(funcBodyStart);
                    int indent = Sci.GetLineIndentation(ln);
                    if (lineFrom == lineTo || lineFrom == ln)
                    {
                        Sci.InsertText(funcBodyStart, Sci.NewLineMarker);
                        Sci.SetLineIndentation(ln + 1, indent);
                        ln++;
                    }
                    Sci.SetLineIndentation(ln, indent + Sci.Indent);
                    Sci.InsertText(funcBodyStart, Sci.NewLineMarker);
                    Sci.SetLineIndentation(ln + 1, indent);
                    Sci.SetLineIndentation(ln, indent + Sci.Indent);
                    funcBodyStart = Sci.LineEndPosition(ln);
                    break;
                }
                else if (!characterClass.Contains(c))
                {
                    break;
                }
                else if (Sci.EOLMode == 1 && c == '\r' && (++nCount) > extraLine)
                {
                    break;
                }
                else if (c == '\n' && (++nCount) > extraLine)
                {
                    if (Sci.EOLMode != 2)
                    {
                        funcBodyStart--;
                    }
                    break;
                }
            }
            return funcBodyStart;
        }