Ejemplo n.º 1
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;
		}