public override bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settings, out CodeString result) { result = null; if (e.Character != '\r' || (!settings?.SmartConcat.IsEnabled ?? true)) { return(false); } var currentContent = CodePaneHandler.GetCurrentLogicalLine(e.Module); if ((!currentContent?.IsInsideStringLiteral ?? true) || currentContent.Lines.Length >= settings.SmartConcat.ConcatMaxLines) { // selection spans more than a single logical line, or spans too many lines to be legal; // too many line continuations throws COMException if we attempt to modify. return(false); } var lastIndexLeftOfCaret = currentContent.CaretLine.Length > 2 ? currentContent.CaretLine.Substring(0, currentContent.CaretPosition.StartColumn).LastIndexOf('"') : 0; if (lastIndexLeftOfCaret > 0) { var indent = currentContent.CaretLine.NthIndexOf('"', 1); var whitespace = new string(' ', indent); // todo: handle shift modifier? var concatVbNewLine = settings.SmartConcat.ConcatVbNewLineModifier.HasFlag(ModifierKeySetting.CtrlKey) && e.IsControlKeyDown; var autoCode = $"\" {(concatVbNewLine ? "& vbNewLine " : string.Empty)}& _\r\n{whitespace}\""; var left = currentContent.CaretLine.Substring(0, currentContent.CaretPosition.StartColumn); var right = currentContent.CaretLine.Substring(currentContent.CaretPosition.StartColumn); var caretLine = $"{left}{autoCode}{right}"; var lines = currentContent.Lines; lines[currentContent.CaretPosition.StartLine] = caretLine; var code = string.Join("\r\n", lines); var newContent = new CodeString(code, currentContent.CaretPosition, currentContent.SnippetPosition); var newPosition = new Selection(newContent.CaretPosition.StartLine + 1, indent + 1); e.Handled = true; result = new CodeString(newContent.Code, newPosition, new Selection(newContent.SnippetPosition.StartLine, 1, newContent.SnippetPosition.EndLine, 1)); CodePaneHandler.SubstituteCode(e.Module, result); var finalSelection = new Selection(result.SnippetPosition.StartLine, 1).Offset(result.CaretPosition); CodePaneHandler.SetSelection(e.Module, finalSelection); return(true); } return(false); }
public void AfterSubstituteCodeTheCorrespondingModuleContainsTheCodeProvided() { var code = "TestTestTest"; var codeModuleMock = new Mock <ICodeModule>(); codeModuleMock.Setup(m => m.Content()).Returns(() => code); codeModuleMock.Setup(m => m.CountOfLines).Returns(1); codeModuleMock.Setup(m => m.Clear()).Callback(() => code = string.Empty); codeModuleMock.Setup(m => m.InsertLines(It.IsAny <int>(), It.IsAny <string>())) .Callback((int start, string str) => code = code.Insert(start - 1, str)); var codeModule = codeModuleMock.Object; var module = new QualifiedModuleName("TestProject", string.Empty, "TestModule"); var projectsProvider = TestProvider(module, codeModule); var codePaneSourceHandler = new CodePaneHandler(projectsProvider); codePaneSourceHandler.SubstituteCode(module, "NewNewNew"); var expected = "NewNewNew"; var actual = codeModule.Content(); Assert.AreEqual(expected, actual); }