Example #1
0
        public static void GenerateExtractMethod(ScintillaNet.ScintillaControl Sci, string NewName)
        {
            FileModel cFile;
            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;
            string template = TemplateUtils.GetTemplate("CallFunction");
            template = TemplateUtils.ReplaceTemplateVariable(template, "Name", NewName);
            template = TemplateUtils.ReplaceTemplateVariable(template, "Arguments", "");

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

            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;
            }

            lookupPosition = Sci.CurrentPos;
            AddLookupPosition();

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

            if (latest == null)
                latest = GetLatestMemberForFunction(found.inClass, ASGenerator.GetDefaultVisibility(), 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, ASGenerator.GetDefaultVisibility());

            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);
        }
Example #2
0
        public static int GetBodyStart(int lineFrom, int lineTo, ScintillaNet.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;
        }
Example #3
0
		static private bool HandleDeclarationCompletion(ScintillaNet.ScintillaControl Sci, string words, string tail, bool autoHide)
		{
			DebugConsole.Trace("** Complete: declaration <"+words+">");
			if (words.Length == 0) 
				return false;
			int position = Sci.CurrentPos;
			
			// concat current word if DotCompletion sent us the previous word
			if (Sci.CharAt(position-1) <= 32) 
				tail = "";
			
			// does it need a tab?
			if ((words != "import") && (words.IndexOf("extends") < 0) && (words.IndexOf("implements") < 0))
			{
				if (ASContext.CurrentClass.IsAS3 && words.IndexOf("var") > 0) {
					words = "const "+words;
				}
				int curLine = Sci.LineFromPosition(position);
				if (Sci.GetLineIndentation(curLine) == 0)
				{
					Sci.SetLineIndentation(curLine, Sci.TabWidth);
					if (Sci.IsUseTabs) position++;
					else position += Sci.TabWidth;
					Sci.SelectionStart = Sci.CurrentPos = position;
				}
			}
			
			// remove keywords already set
			words = " "+words+" ";
			string rem = GetWordLeft(Sci, ref position);
			while (rem.Length > 0)
			{
				if ((rem == "var") || (rem == "function") || (ASContext.CurrentClass.IsAS3 && (rem == "const")))
					return true;
				else if (rem == "public")
					words = words.Replace(" private ", " ");
				else if (rem == "private")
					words = words.Replace(" public ", " ");
				words = words.Replace(" "+rem+" ", " ");
				rem = GetWordLeft(Sci, ref position);
			}
			words = words.Trim();
			
			// build list
			string[] items = words.Split(' ');
			ArrayList known = new ArrayList();
			foreach(string item in items)
				known.Add(new TemplateItem(item));
			
			// show
			CompletionList.Show(known, autoHide, tail);
			return true;
		}
Example #4
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();
     }
 }
Example #5
0
		static private bool HandleStructureCompletion(ScintillaNet.ScintillaControl Sci)
		{
			try
			{
				int position = Sci.CurrentPos;
				int line = Sci.LineFromPosition(position);
				if (line == 0) 
					return false;
				string txt = Sci.GetLine(line-1);
				int style = Sci.BaseStyleAt(position);
				int eolMode = Sci.EOLMode;
				// box comments
				if (IsCommentStyle(style) && (Sci.BaseStyleAt(position+1) == style))
				{
					txt = txt.Trim();
					if (txt.StartsWith("/*") || txt.StartsWith("*"))
					{
						Sci.ReplaceSel("* ");
						position = Sci.LineIndentPosition(line)+2;
						Sci.SetSel(position,position);
						return true;
					}
				}
				// braces
				else if (txt.TrimEnd().EndsWith("{") && (line > 1))
				{
					// find matching brace
					int bracePos = Sci.LineEndPosition(line-1)-1;
					while ((bracePos > 0) && (Sci.CharAt(bracePos) != '{')) bracePos--;
					if (bracePos == 0 || Sci.BaseStyleAt(bracePos) != 10) return true;
					int match = Sci.SafeBraceMatch(bracePos);
					DebugConsole.Trace("match "+bracePos+" "+match);
					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 false;
				 	}
					// find where to include the closing brace
					int startIndent = indent;
					int newIndent = indent+Sci.TabWidth;
					int count = Sci.LineCount;
					int lastLine = line;
					line++;
					while (line < count-1)
					{
						txt = Sci.GetLine(line).TrimEnd();
						if (txt.Length != 0) {
							indent = Sci.GetLineIndentation(line);
							DebugConsole.Trace("indent "+(line+1)+" "+indent+" : "+txt);
							if (indent <= startIndent) break;
							lastLine = line;
						}
						else break;
						line++;
					}
					if (line >= count-1) lastLine = start;
					
					// insert closing brace
					DebugConsole.Trace("Insert at "+position);
					position = Sci.LineEndPosition(lastLine);
					Sci.InsertText(position, ASContext.MainForm.GetNewLineMarker(eolMode)+"}");
					Sci.SetLineIndentation(lastLine+1, startIndent);
					return false;
				}
			}
			catch (Exception ex)
			{
				ErrorHandler.ShowError(ex.Message, ex);
			}
			return false;
		}