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; }
public static void GenerateExtractVariable(ScintillaNet.ScintillaControl Sci, string NewName) { FileModel cFile; IASContext context = ASContext.Context; Int32 pos = Sci.CurrentPos; string expression = Sci.SelText.Trim(new char[] { '=', ' ', '\t', '\n', '\r', ';', '.' }); expression = expression.TrimEnd(new char[] { '(', '[', '{', '<' }); expression = expression.TrimStart(new char[] { ')', ']', '}', '>' }); cFile = ASContext.Context.CurrentModel; ASFileParser parser = new ASFileParser(); parser.ParseSrc(cFile, Sci.Text); MemberModel current = cFile.Context.CurrentMember; string characterClass = ScintillaNet.ScintillaControl.Configuration.GetLanguage(Sci.ConfigurationLanguage).characterclass.Characters; int funcBodyStart = ASGenerator.GetBodyStart(current.LineFrom, current.LineTo, Sci); Sci.SetSel(funcBodyStart, Sci.LineEndPosition(current.LineTo)); string currentMethodBody = Sci.SelText; bool isExprInSingleQuotes = (expression.StartsWith("'") && expression.EndsWith("'")); bool isExprInDoubleQuotes = (expression.StartsWith("\"") && expression.EndsWith("\"")); int stylemask = (1 << Sci.StyleBits) - 1; int lastPos = -1; char prevOrNextChar; Sci.Colourise(0, -1); while (true) { lastPos = currentMethodBody.IndexOf(expression, lastPos + 1); if (lastPos > -1) { if (lastPos > 0) { prevOrNextChar = currentMethodBody[lastPos - 1]; if (characterClass.IndexOf(prevOrNextChar) > -1) { continue; } } if (lastPos + expression.Length < currentMethodBody.Length) { prevOrNextChar = currentMethodBody[lastPos + expression.Length]; if (characterClass.IndexOf(prevOrNextChar) > -1) { continue; } } int style = Sci.StyleAt(funcBodyStart + lastPos) & stylemask; if (ASComplete.IsCommentStyle(style)) { continue; } else if ((isExprInDoubleQuotes && currentMethodBody[lastPos] == '"' && currentMethodBody[lastPos + expression.Length - 1] == '"') || (isExprInSingleQuotes && currentMethodBody[lastPos] == '\'' && currentMethodBody[lastPos + expression.Length - 1] == '\'')) { } else if (!ASComplete.IsTextStyle(style)) { continue; } Sci.SetSel(funcBodyStart + lastPos, funcBodyStart + lastPos + expression.Length); Sci.ReplaceSel(NewName); currentMethodBody = currentMethodBody.Substring(0, lastPos) + NewName + currentMethodBody.Substring(lastPos + expression.Length); lastPos += NewName.Length; } else { break; } } Sci.CurrentPos = funcBodyStart; Sci.SetSel(Sci.CurrentPos, Sci.CurrentPos); MemberModel m = new MemberModel(NewName, "", FlagType.LocalVar, 0); m.Value = expression; string snippet = TemplateUtils.GetTemplate("Variable"); snippet = TemplateUtils.ReplaceTemplateVariable(snippet, "Modifiers", null); snippet = TemplateUtils.ToDeclarationString(m, snippet); snippet += NewLine + "$(Boundary)"; SnippetHelper.InsertSnippetText(Sci, Sci.CurrentPos, snippet); }
private static void AddAsParameter(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member, bool detach) { if (!RemoveLocalDeclaration(Sci, contextMember)) return; int posStart = Sci.PositionFromLine(member.LineFrom); int posEnd = Sci.LineEndPosition(member.LineTo); Sci.SetSel(posStart, posEnd); string selectedText = Sci.SelText; Regex rStart = new Regex(@"\s{1}" + member.Name + @"\s*\(([^\)]*)\)(\s*:\s*([^({{|\n|\r|\s|;)]+))?"); Match mStart = rStart.Match(selectedText); if (!mStart.Success) return; int start = mStart.Index + posStart + 1; int end = mStart.Index + posStart + mStart.Length; Sci.SetSel(start, end); MemberModel memberCopy = (MemberModel) member.Clone(); if (memberCopy.Parameters == null) memberCopy.Parameters = new List<MemberModel>(); memberCopy.Parameters.Add(contextMember); string template = TemplateUtils.ToDeclarationString(memberCopy, TemplateUtils.GetTemplate("MethodDeclaration")); InsertCode(start, template); int currPos = Sci.LineEndPosition(Sci.CurrentLine); Sci.SetSel(currPos, currPos); Sci.CurrentPos = currPos; }
private static void GenerateFieldFromParameter(ScintillaNet.ScintillaControl Sci, MemberModel member, ClassModel inClass, Visibility scope) { int funcBodyStart = GetBodyStart(member.LineFrom, member.LineTo, Sci); Sci.SetSel(funcBodyStart, Sci.LineEndPosition(member.LineTo)); string body = Sci.SelText; string trimmed = body.TrimStart(); Regex re = new Regex("^super\\s*[\\(|\\.]"); Match m = re.Match(trimmed); if (m.Success) { if (m.Index == 0) { int p = funcBodyStart + (body.Length - trimmed.Length); int l = Sci.LineFromPosition(p); p = Sci.PositionFromLine(l + 1); funcBodyStart = GetBodyStart(member.LineFrom, member.LineTo, Sci, p); } } Sci.SetSel(funcBodyStart, funcBodyStart); Sci.CurrentPos = funcBodyStart; bool isVararg = false; string paramName = contextMember.Name; if (paramName.StartsWith("...")) { paramName = paramName.TrimStart(new char[] { ' ', '.' }); isVararg = true; } string varName = paramName; string scopedVarName = varName; if ((scope & Visibility.Public) > 0) { if ((member.Flags & FlagType.Static) > 0) scopedVarName = inClass.Name + "." + varName; else scopedVarName = "this." + varName; } else { if (ASContext.CommonSettings.PrefixFields.Length > 0 && !paramName.StartsWith(ASContext.CommonSettings.PrefixFields)) { scopedVarName = varName = ASContext.CommonSettings.PrefixFields + varName; } if (ASContext.CommonSettings.GenerateScope || ASContext.CommonSettings.PrefixFields == "") { if ((member.Flags & FlagType.Static) > 0) scopedVarName = inClass.Name + "." + varName; else scopedVarName = "this." + varName; } } string template = TemplateUtils.GetTemplate("FieldFromParameter"); template = TemplateUtils.ReplaceTemplateVariable(template, "Name", scopedVarName); template = TemplateUtils.ReplaceTemplateVariable(template, "Value", paramName); template += "\n$(Boundary)"; SnippetHelper.InsertSnippetText(Sci, funcBodyStart, template); MemberList classMembers = inClass.Members; foreach (MemberModel classMember in classMembers) if (classMember.Name.Equals(varName)) return; MemberModel latest = GetLatestMemberForVariable(GeneratorJobType.Variable, inClass, GetDefaultVisibility(), new MemberModel()); if (latest == null) return; int position = FindNewVarPosition(Sci, inClass, latest); if (position <= 0) return; Sci.SetSel(position, position); Sci.CurrentPos = position; MemberModel mem = NewMember(varName, member, FlagType.Variable, scope); if (isVararg) mem.Type = "Array"; else mem.Type = contextMember.Type; GenerateVariable(mem, position, true); ASContext.Panel.RestoreLastLookupPosition(); }
private static void ChangeDecl(ScintillaNet.ScintillaControl Sci, MemberModel memberModel, List<FunctionParameter> functionParameters) { bool paramsDiffer = false; if (memberModel.Parameters != null) { // check that parameters have one and the same type if (memberModel.Parameters.Count == functionParameters.Count) { if (functionParameters.Count > 0) { List<MemberModel> parameters = memberModel.Parameters; for (int i = 0; i < parameters.Count; i++) { MemberModel p = parameters[i]; if (p.Type != functionParameters[i].paramType) { paramsDiffer = true; break; } } } } else { paramsDiffer = true; } } // check that parameters count differs else if (functionParameters.Count != 0) { paramsDiffer = true; } if (paramsDiffer) { int app = 0; List<MemberModel> newParameters = new List<MemberModel>(); List<MemberModel> existingParameters = memberModel.Parameters; for (int i = 0; i < functionParameters.Count; i++) { FunctionParameter p = functionParameters[i]; if (existingParameters != null && existingParameters.Count > (i - app) && existingParameters[i - app].Type == p.paramType) { newParameters.Add(existingParameters[i - app]); } else { if (existingParameters != null && existingParameters.Count < functionParameters.Count) { app++; } newParameters.Add(new MemberModel(p.paramName, p.paramType, FlagType.ParameterVar, 0)); } } memberModel.Parameters = newParameters; int posStart = Sci.PositionFromLine(memberModel.LineFrom); int posEnd = Sci.LineEndPosition(memberModel.LineTo); Sci.SetSel(posStart, posEnd); string selectedText = Sci.SelText; Regex rStart = new Regex(@"\s{1}" + memberModel.Name + @"\s*\(([^\)]*)\)(\s*:\s*([^({{|\n|\r|\s|;)]+))?"); Match mStart = rStart.Match(selectedText); if (!mStart.Success) { return; } int start = mStart.Index + posStart; int end = start + mStart.Length; Sci.SetSel(start, end); string decl = TemplateUtils.ToDeclarationString(memberModel, TemplateUtils.GetTemplate("MethodDeclaration")); InsertCode(Sci.CurrentPos, "$(Boundary) " + decl); // add imports to function argument types if (functionParameters.Count > 0) { List<string> l = new List<string>(); foreach (FunctionParameter fp in functionParameters) { try { l.Add(fp.paramQualType); } catch (Exception) { } } start += AddImportsByName(l, Sci.LineFromPosition(end)); } Sci.SetSel(start, start); } }
private static void GenerateFieldFromParameter(ScintillaNet.ScintillaControl Sci, MemberModel member, ClassModel inClass) { int funcBodyStart = GetBodyStart(member.LineFrom, member.LineTo, Sci); Sci.SetSel(funcBodyStart, Sci.LineEndPosition(member.LineTo)); string body = Sci.SelText; string trimmed = body.TrimStart(); Regex re = new Regex("^super\\s*[\\(|\\.]"); Match m = re.Match(trimmed); if (m.Success) { if (m.Index == 0) { int p = funcBodyStart + (body.Length - trimmed.Length); int l = Sci.LineFromPosition(p); p = Sci.PositionFromLine(l + 1); funcBodyStart = GetBodyStart(member.LineFrom, member.LineTo, Sci, p); } } Sci.SetSel(funcBodyStart, funcBodyStart); Sci.CurrentPos = funcBodyStart; bool isVararg = false; string paramName = contextMember.Name; if (paramName.StartsWith("...")) { paramName = paramName.TrimStart(new char[] { ' ', '.' }); isVararg = true; } string varName = paramName; bool addThis = true; if (ASContext.CommonSettings.PrefixFields.Length > 0 && !paramName.StartsWith(ASContext.CommonSettings.PrefixFields)) { varName = ASContext.CommonSettings.PrefixFields + varName; addThis = ASContext.CommonSettings.GenerateScope; } StringBuilder sb = new StringBuilder(); if (addThis) sb.Append("this."); sb.Append(varName) .Append(" = ") .Append(paramName) .Append(";") .Append(NewLine) .Append("$(Boundary)"); SnippetHelper.InsertSnippetText(Sci, funcBodyStart, sb.ToString()); MemberList classMembers = inClass.Members; foreach (MemberModel classMember in classMembers) { if (classMember.Name.Equals(varName)) { return; } } MemberModel latest = FindLatest(FlagType.Variable | FlagType.Constant, GetDefaultVisibility(), inClass); if (latest == null) return; int position = FindNewVarPosition(Sci, inClass, latest); if (position <= 0) return; Sci.SetSel(position, position); Sci.CurrentPos = position; MemberModel mem = NewMember(varName, member, FlagType.Variable, GetDefaultVisibility()); if (isVararg) { mem.Type = "Array"; } else { mem.Type = contextMember.Type; } GenerateVariable(mem, position, true); ASContext.Panel.RestoreLastLookupPosition(); }
private static void AddAsParameter(ClassModel inClass, ScintillaNet.ScintillaControl Sci, MemberModel member, bool detach) { if (!RemoveLocalDeclaration(Sci, contextMember)) return; StringBuilder sb = new StringBuilder(); int position = Sci.PositionFromLine(member.LineFrom); Sci.SetSel(position, Sci.LineEndPosition(member.LineTo)); string body = Sci.SelText; int bracePos = body.IndexOf(')') + position; if (member.Parameters != null && member.Parameters.Count > 0) { sb.Append(", "); } sb.Append(contextMember.Name); if (contextMember.Type != null) { sb.Append(":") .Append(contextMember.Type); } Sci.SetSel(bracePos, bracePos); Sci.CurrentPos = bracePos; InsertCode(bracePos, sb.ToString()); Sci.SetSel(lookupPosition, lookupPosition); }
private static void ChangeMethodDecl(ScintillaNet.ScintillaControl Sci, MemberModel member, ClassModel inClass) { int wordPos = Sci.WordEndPosition(Sci.CurrentPos, true); List<FunctionParameter> functionParameters = ParseFunctionParameters(Sci, wordPos); ASResult funcResult = ASComplete.GetExpressionType(Sci, Sci.WordEndPosition(Sci.CurrentPos, true)); if (funcResult.Member == null) { return; } if (funcResult != null && funcResult.inClass != null && !funcResult.inClass.Equals(inClass)) { AddLookupPosition(); lookupPosition = -1; DockContent dc = ASContext.MainForm.OpenEditableDocument(funcResult.inClass.InFile.FileName, true); Sci = ASContext.CurSciControl; FileModel fileModel = new FileModel(); ASFileParser parser = new ASFileParser(); parser.ParseSrc(fileModel, Sci.Text); foreach (ClassModel cm in fileModel.Classes) { if (cm.QualifiedName.Equals(funcResult.inClass.QualifiedName)) { funcResult.inClass = cm; break; } } inClass = funcResult.inClass; } MemberList members = inClass.Members; foreach (MemberModel m in members) { if (m.Name == funcResult.Member.Name) { funcResult.Member = m; } } bool paramsDiffer = false; if (funcResult.Member.Parameters != null) { // check that parameters have one and the same type if (funcResult.Member.Parameters.Count == functionParameters.Count) { if (functionParameters.Count > 0) { List<MemberModel> parameters = funcResult.Member.Parameters; for (int i = 0; i < parameters.Count; i++) { MemberModel p = parameters[i]; if (p.Type != functionParameters[i].paramType) { paramsDiffer = true; break; } } } } else { paramsDiffer = true; } } // check that parameters count differs else if (functionParameters.Count != 0) { paramsDiffer = true; } if (paramsDiffer) { int app = 0; List<MemberModel> newParameters = new List<MemberModel>(); List<MemberModel> existingParameters = funcResult.Member.Parameters; for (int i = 0; i < functionParameters.Count; i++) { FunctionParameter p = functionParameters[i]; if (existingParameters != null && existingParameters.Count > (i - app) && existingParameters[i - app].Type == p.paramType) { newParameters.Add(existingParameters[i - app]); } else { if (existingParameters != null && existingParameters.Count < functionParameters.Count) { app++; } newParameters.Add(new MemberModel(p.param, p.paramType, FlagType.ParameterVar, 0)); } } funcResult.Member.Parameters = newParameters; int posStart = Sci.PositionFromLine(funcResult.Member.LineFrom); int posEnd = Sci.LineEndPosition(funcResult.Member.LineTo); Sci.SetSel(posStart, posEnd); string selectedText = Sci.SelText; Regex rStart = new Regex(String.Format(@"\s+{0}\s*\(([^\)]*)\)(\s*:\s*([^({{|\n|\r|\s|;)]+))?", funcResult.Member.Name)); Match mStart = rStart.Match(selectedText); if (!mStart.Success) { return; } int start = mStart.Index + posStart; int end = start + mStart.Length; Sci.SetSel(start, end); string decl = funcResult.Member.ToDeclarationString(); Sci.ReplaceSel(" " + decl); // add imports to function argument types if (functionParameters.Count > 0) { List<string> l = new List<string>(); foreach (FunctionParameter fp in functionParameters) { try { l.Add(fp.paramQualType); } catch (Exception) { } } start += AddImportsByName(l, Sci.LineFromPosition(end)); } Sci.SetSel(start, start); } }
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; }