private void RewriteSignature(Declaration target, VBAParser.ArgListContext paramList, ICodeModule module) { var parameters = paramList.arg().Select((s, i) => new { Index = i, Text = s.GetText() }).ToList(); var reorderedParams = new List <string>(); foreach (var param in _model.Parameters) { var parameterAtIndex = parameters.SingleOrDefault(s => s.Index == param.Index); if (parameterAtIndex != null) { reorderedParams.Add(parameterAtIndex.Text); } } // property let/set and paramarrays for (var index = reorderedParams.Count; index < parameters.Count; index++) { reorderedParams.Add(parameters[index].Text); } var signature = GetOldSignature(target); signature = signature.Remove(signature.IndexOf('(')); var asTypeText = target.AsTypeContext == null ? string.Empty : " " + target.AsTypeContext.GetText(); signature += '(' + string.Join(", ", reorderedParams) + ")" + (asTypeText == " " ? string.Empty : asTypeText); var lineCount = paramList.GetSelection().LineCount; module.ReplaceLine(paramList.Start.Line, signature.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private void SetFieldToPrivate(ICodeModule module) { if (_model.TargetDeclaration.Accessibility == Accessibility.Private) { return; } RemoveField(_model.TargetDeclaration); var newField = "Private " + _model.TargetDeclaration.IdentifierName + " As " + _model.TargetDeclaration.AsTypeName; module.InsertLines(module.CountOfDeclarationLines + 1, newField); var pane = module.CodePane; { pane.Selection = _model.TargetDeclaration.QualifiedSelection.Selection; } for (var index = 1; index <= module.CountOfDeclarationLines; index++) { if (module.GetLines(index, 1).Trim() == string.Empty) { module.DeleteLines(new Selection(index, 0, index, 0)); } } }
private void AddParameter(Declaration targetMethod, Declaration targetVariable, VBAParser.ArgListContext paramList, ICodeModule module) { var argList = paramList.arg(); var lastParam = argList.LastOrDefault(); var newContent = GetOldSignature(targetMethod); if (lastParam == null) { // Increase index by one because VBA is dumb enough to use 1-based indexing newContent = newContent.Insert(newContent.IndexOf('(') + 1, GetParameterDefinition(targetVariable)); } else if (targetMethod.DeclarationType != DeclarationType.PropertyLet && targetMethod.DeclarationType != DeclarationType.PropertySet) { newContent = newContent.Replace(argList.Last().GetText(), argList.Last().GetText() + ", " + GetParameterDefinition(targetVariable)); } else { newContent = newContent.Replace(argList.Last().GetText(), GetParameterDefinition(targetVariable) + ", " + argList.Last().GetText()); } module.ReplaceLine(paramList.Start.Line, newContent); module.DeleteLines(paramList.Start.Line + 1, paramList.GetSelection().LineCount - 1); }
public virtual void RemoveSelection(ICodeModule codeModule, IEnumerable <Selection> selection) { foreach (var item in selection.OrderBy(x => - x.StartLine)) { var start = item.StartLine; var end = item.EndLine; var lineCount = end - start + 1; codeModule.DeleteLines(start, lineCount); } }
private static void RemoveDeclarationOnly(this ICodeModule module, Declaration target) { var multipleDeclarations = target.DeclarationType == DeclarationType.Variable && target.HasMultipleDeclarationsInStatement(); var context = GetStmtContext(target); var declarationText = context.GetText().Replace(" _" + Environment.NewLine, Environment.NewLine); var selection = GetStmtContextSelection(target); Debug.Assert(selection.StartColumn > 0); var oldLines = module.GetLines(selection); var indent = oldLines.IndexOf(oldLines.FirstOrDefault(c => c != ' ')) + 1; var newLines = oldLines .Replace(" _" + Environment.NewLine, Environment.NewLine) .Remove(selection.StartColumn - 1, declarationText.Length - selection.StartColumn + indent); if (multipleDeclarations) { selection = GetStmtContextSelection(target); newLines = RemoveExtraComma(module.GetLines(selection).Replace(oldLines, newLines), target.CountOfDeclarationsInStatement(), target.IndexOfVariableDeclarationInStatement()); } var newLinesWithoutExcessSpaces = newLines.Split(new[] { Environment.NewLine }, StringSplitOptions.None); for (var i = 0; i < newLinesWithoutExcessSpaces.Length; i++) { newLinesWithoutExcessSpaces[i] = newLinesWithoutExcessSpaces[i].RemoveExtraSpacesLeavingIndentation(); } for (var i = newLinesWithoutExcessSpaces.Length - 1; i >= 0; i--) { if (newLinesWithoutExcessSpaces[i].Trim() == string.Empty) { continue; } if (newLinesWithoutExcessSpaces[i].EndsWith(" _")) { newLinesWithoutExcessSpaces[i] = newLinesWithoutExcessSpaces[i].Remove(newLinesWithoutExcessSpaces[i].Length - 2); } break; } // remove all lines with only whitespace newLinesWithoutExcessSpaces = newLinesWithoutExcessSpaces.Where(str => str.Any(c => !char.IsWhiteSpace(c))).ToArray(); module.DeleteLines(selection); if (newLinesWithoutExcessSpaces.Any()) { module.InsertLines(selection.StartLine, string.Join(Environment.NewLine, newLinesWithoutExcessSpaces)); } }
private void RewriteCall(VBAParser.ArgumentListContext paramList, ICodeModule module) { var argValues = new List <string>(); if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { argValues.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => { if (p is VBAParser.SpecifiedPositionalArgumentContext) { return(((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText()); } return(string.Empty); }).ToList()); } if (paramList.positionalOrNamedArgumentList().namedArgumentList() != null) { argValues.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { argValues.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.GetLines(paramList.Start.Line, lineCount); newContent = newContent.Remove(paramList.Start.Column, paramList.GetText().Length); var reorderedArgValues = new List <string>(); foreach (var param in _model.Parameters) { var argAtIndex = argValues.ElementAtOrDefault(param.Index); if (argAtIndex != null) { reorderedArgValues.Add(argAtIndex); } } // property let/set and paramarrays for (var index = reorderedArgValues.Count; index < argValues.Count; index++) { reorderedArgValues.Add(argValues[index]); } newContent = newContent.Insert(paramList.Start.Column, string.Join(", ", reorderedArgValues)); module.ReplaceLine(paramList.Start.Line, newContent.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
public void SubstituteCode(ICodeModule module, CodeString newCode) { try { module.DeleteLines(newCode.SnippetPosition); module.InsertLines(newCode.SnippetPosition.StartLine, newCode.Code); } catch { Debug.Assert(false, "too many line continuations. we shouldn't even be here."); } }
public static void Remove(this ICodeModule module, Selection selection, ParserRuleContext instruction) { var originalCodeLines = module.GetLines(selection.StartLine, selection.LineCount); var originalInstruction = instruction.GetText(); module.DeleteLines(selection.StartLine, selection.LineCount); var newCodeLines = originalCodeLines.Replace(originalInstruction, string.Empty); if (!string.IsNullOrEmpty(newCodeLines)) { module.InsertLines(selection.StartLine, newCodeLines); } }
private void RemoveSignatureParameters(Declaration target, VBAParser.ArgListContext paramList, ICodeModule module) { // property set/let have one more parameter than is listed in the getter parameters var nonRemovedParamNames = paramList.arg().Where((a, s) => s >= _model.Parameters.Count || !_model.Parameters[s].IsRemoved).Select(s => s.GetText()); var signature = GetOldSignature(target); signature = signature.Remove(signature.IndexOf('(')); var asTypeText = target.AsTypeContext == null ? string.Empty : " " + target.AsTypeContext.GetText(); signature += '(' + string.Join(", ", nonRemovedParamNames) + ")" + (asTypeText == " " ? string.Empty : asTypeText); var lineCount = paramList.GetSelection().LineCount; module.ReplaceLine(paramList.Start.Line, signature.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }
private void HandleSelfClosingPairs(AutoCompleteEventArgs e, ICodeModule module, Selection pSelection) { if (!pSelection.IsSingleCharacter) { return; } var currentCode = e.CurrentLine; var currentSelection = e.CurrentSelection; //var surroundingCode = GetSurroundingCode(module, currentSelection); // todo: find a way to parse the current instruction var original = new CodeString(currentCode, new Selection(0, currentSelection.EndColumn - 1), new Selection(pSelection.StartLine, 1)); var prettifier = new CodeStringPrettifier(module); foreach (var selfClosingPair in _selfClosingPairs) { CodeString result; if (e.Character == '\b' && pSelection.StartColumn > 1) { result = _selfClosingPairCompletion.Execute(selfClosingPair, original, '\b'); } else { result = _selfClosingPairCompletion.Execute(selfClosingPair, original, e.Character, prettifier); } if (result != default) { using (var pane = module.CodePane) { module.DeleteLines(result.SnippetPosition); module.InsertLines(result.SnippetPosition.StartLine, result.Code); pane.Selection = result.SnippetPosition.Offset(result.CaretPosition); e.Handled = true; return; } } } }
public bool IsSpacingUnchanged(CodeString code, CodeString original) { using (var pane = _module.CodePane) { using (var window = pane.Window) { //window.ScreenUpdating = false; _module.DeleteLines(code.SnippetPosition); _module.InsertLines(code.SnippetPosition.StartLine, code.Code); //window.ScreenUpdating = true; pane.Selection = code.SnippetPosition.Offset(code.CaretPosition); var lines = _module.GetLines(code.SnippetPosition); if (lines.Equals(code.Code, StringComparison.InvariantCultureIgnoreCase)) { return(true); } } _module.ReplaceLine(code.SnippetPosition.StartLine, original.Code); } return(false); }
public CodeString Prettify(ICodeModule module, CodeString original) { var originalCode = original.Code.Replace("\r", string.Empty).Split('\n'); var originalPosition = original.CaretPosition.StartColumn; var isAtLastCharacter = originalPosition == original.CaretLine.Length; var originalNonWhitespaceCharacters = 0; var isAllWhitespace = !isAtLastCharacter; if (!isAtLastCharacter) { for (var i = 0; i <= Math.Min(originalPosition - 1, original.CaretLine.Length - 1); i++) { if (originalCode[original.CaretPosition.StartLine][i] != ' ') { originalNonWhitespaceCharacters++; isAllWhitespace = false; } } } var indent = original.CaretLine.TakeWhile(c => c == ' ').Count(); module.DeleteLines(original.SnippetPosition.StartLine, original.SnippetPosition.LineCount); module.InsertLines(original.SnippetPosition.StartLine, string.Join("\r\n", originalCode)); var prettifiedCode = module.GetLines(original.SnippetPosition) .Replace("\r", string.Empty) .Split('\n'); var prettifiedNonWhitespaceCharacters = 0; var prettifiedCaretCharIndex = 0; if (!isAtLastCharacter) { for (var i = 0; i < prettifiedCode[original.CaretPosition.StartLine].Length; i++) { if (prettifiedCode[original.CaretPosition.StartLine][i] != ' ') { prettifiedNonWhitespaceCharacters++; if (prettifiedNonWhitespaceCharacters == originalNonWhitespaceCharacters || i == prettifiedCode[original.CaretPosition.StartLine].Length - 1) { prettifiedCaretCharIndex = i; break; } } } } else { prettifiedCaretCharIndex = prettifiedCode[original.CaretPosition.StartLine].Length; } var prettifiedPosition = new Selection( original.SnippetPosition.ToZeroBased().StartLine + original.CaretPosition.StartLine, prettifiedCode[original.CaretPosition.StartLine].Trim().Length == 0 || (isAllWhitespace && !string.IsNullOrEmpty(original.CaretLine.Substring(original.CaretPosition.StartColumn).Trim())) ? Math.Min(indent, original.CaretPosition.StartColumn) : Math.Min(prettifiedCode[original.CaretPosition.StartLine].Length, prettifiedCaretCharIndex + 1)) .ToOneBased(); SetSelection(module, prettifiedPosition); return(GetPrettifiedCodeString(original, prettifiedPosition, prettifiedCode)); }
private void RemoveCallParameter(VBAParser.ArgumentListContext paramList, ICodeModule module) { var paramNames = new List <string>(); if (paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().positionalArgumentOrMissing().Select(p => { if (p is VBAParser.SpecifiedPositionalArgumentContext) { return(((VBAParser.SpecifiedPositionalArgumentContext)p).positionalArgument().GetText()); } return(string.Empty); }).ToList()); } if (paramList.positionalOrNamedArgumentList().namedArgumentList() != null) { paramNames.AddRange(paramList.positionalOrNamedArgumentList().namedArgumentList().namedArgument().Select(p => p.GetText()).ToList()); } if (paramList.positionalOrNamedArgumentList().requiredPositionalArgument() != null) { paramNames.Add(paramList.positionalOrNamedArgumentList().requiredPositionalArgument().GetText()); } var lineCount = paramList.Stop.Line - paramList.Start.Line + 1; // adjust for total line count var newContent = module.GetLines(paramList.Start.Line, lineCount); newContent = newContent.Remove(paramList.Start.Column, paramList.GetText().Length); var savedParamNames = paramNames; for (var index = _model.Parameters.Count - 1; index >= 0; index--) { var param = _model.Parameters[index]; if (!param.IsRemoved) { continue; } if (param.Name.Contains("ParamArray")) { // handle param arrays while (savedParamNames.Count > index) { savedParamNames.RemoveAt(index); } } else { if (index < savedParamNames.Count && !savedParamNames[index].StripStringLiterals().Contains(":=")) { savedParamNames.RemoveAt(index); } else { var paramIndex = savedParamNames.FindIndex(s => s.StartsWith(param.Declaration.IdentifierName + ":=")); if (paramIndex != -1 && paramIndex < savedParamNames.Count) { savedParamNames.RemoveAt(paramIndex); } } } } newContent = newContent.Insert(paramList.Start.Column, string.Join(", ", savedParamNames)); module.ReplaceLine(paramList.Start.Line, newContent.Replace(" _" + Environment.NewLine, string.Empty)); module.DeleteLines(paramList.Start.Line + 1, lineCount - 1); }