/// <summary> /// Called from VS when we should handle a command or pass it on. /// </summary> public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { // preprocessing if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97) { switch ((VSConstants.VSStd97CmdID)nCmdID) { case VSConstants.VSStd97CmdID.Paste: string updated = RemoveReplPrompts(_textView.Options.GetNewLineCharacter()); if (updated != null) { _editorOps.ReplaceSelection(updated); return(VSConstants.S_OK); } break; case VSConstants.VSStd97CmdID.GotoDefn: return(GotoDefinition()); case VSConstants.VSStd97CmdID.FindReferences: return(FindAllReferences()); } } else if (pguidCmdGroup == CommonConstants.Std2KCmdGroupGuid) { OutliningTaggerProvider.OutliningTagger tagger; switch ((VSConstants.VSStd2KCmdID)nCmdID) { case VSConstants.VSStd2KCmdID.RETURN: if (PythonToolsPackage.Instance.LangPrefs.IndentMode == vsIndentStyle.vsIndentStyleSmart) { // smart indent AutoIndent.HandleReturn(_textView); return(VSConstants.S_OK); } break; case VSConstants.VSStd2KCmdID.BACKSPACE: if (PythonToolsPackage.Instance.LangPrefs.IndentMode == vsIndentStyle.vsIndentStyleSmart && _textView.Selection.IsEmpty) { int indentSize = _textView.Options.GetIndentSize(); // smart dedent var containingLine = _textView.Caret.Position.BufferPosition.GetContainingLine(); var curLineLine = containingLine.GetText(); int lineOffset = _textView.Caret.Position.BufferPosition.Position - containingLine.Start.Position; if (lineOffset >= indentSize) { bool allSpaces = true; for (int i = lineOffset - 1; i >= lineOffset - indentSize; i--) { if (curLineLine[i] != ' ') { allSpaces = false; break; } } if (allSpaces) { _textView.TextBuffer.Delete(new Span(_textView.Caret.Position.BufferPosition.Position - indentSize, indentSize)); return(VSConstants.S_OK); } } } break; case VSConstants.VSStd2KCmdID.SHOWMEMBERLIST: case VSConstants.VSStd2KCmdID.COMPLETEWORD: var controller = _textView.Properties.GetProperty <IntellisenseController>(typeof(IntellisenseController)); if (controller != null) { controller.TriggerCompletionSession((VSConstants.VSStd2KCmdID)nCmdID == VSConstants.VSStd2KCmdID.COMPLETEWORD); return(VSConstants.S_OK); } break; case VSConstants.VSStd2KCmdID.OUTLN_STOP_HIDING_ALL: tagger = _textView.TextBuffer.GetOutliningTagger(); if (tagger != null) { tagger.Disable(); } // let VS get the event as well break; case VSConstants.VSStd2KCmdID.OUTLN_START_AUTOHIDING: tagger = _textView.TextBuffer.GetOutliningTagger(); if (tagger != null) { tagger.Enable(); } // let VS get the event as well break; case VSConstants.VSStd2KCmdID.COMMENT_BLOCK: case VSConstants.VSStd2KCmdID.COMMENTBLOCK: _textView.CommentBlock(); break; case VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK: case VSConstants.VSStd2KCmdID.UNCOMMENTBLOCK: _textView.UncommentBlock(); break; } } else if (pguidCmdGroup == GuidList.guidPythonToolsCmdSet) { foreach (var command in PythonToolsPackage.Commands) { if (command.CommandId == nCmdID) { command.DoCommand(this, EventArgs.Empty); return(VSConstants.S_OK); } } } return(_next.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut)); }
private void FormatCode(SnapshotSpan span, bool selectResult) { // get the line numbers for the start and end of the span int startLine = _textView.TextBuffer.CurrentSnapshot.GetLineNumberFromPosition(span.Start.Position); int endLine = _textView.TextBuffer.CurrentSnapshot.GetLineNumberFromPosition(span.End.Position); AutoIndent.Format(_textView, startLine, endLine); }
public int?GetDesiredIndentation(ITextSnapshotLine line) { if (_provider._pyService.LangPrefs.IndentMode == vsIndentStyle.vsIndentStyleSmart) { return(AutoIndent.GetLineIndentation(_provider._editorServices.GetBufferInfo(line.Snapshot.TextBuffer), line, _textView)); } else { return(null); } }
public void GetIndentation() { Assert.AreEqual(0, AutoIndent.GetIndentation("", 4)); Assert.AreEqual(0, AutoIndent.GetIndentation("p", 4)); Assert.AreEqual(4, AutoIndent.GetIndentation(" ", 4)); Assert.AreEqual(4, AutoIndent.GetIndentation(" p", 4)); Assert.AreEqual(3, AutoIndent.GetIndentation(" p", 4)); Assert.AreEqual(5, AutoIndent.GetIndentation(" p", 4)); Assert.AreEqual(4, AutoIndent.GetIndentation("\tp", 4)); Assert.AreEqual(5, AutoIndent.GetIndentation("\t p", 4)); Assert.AreEqual(5, AutoIndent.GetIndentation(" \tp", 4)); Assert.AreEqual(6, AutoIndent.GetIndentation(" \t p", 4)); }
private static void AssertIndent(string code, int lineNumber, int expected, int tabSize = 4, int indentSize = 4) { var buffer = new MockTextBuffer(code, PythonContentType); var view = new MockTextView(buffer); view.Options.SetOptionValue(DefaultOptions.IndentSizeOptionId, indentSize); view.Options.SetOptionValue(DefaultOptions.TabSizeOptionId, tabSize); var line = buffer.CurrentSnapshot.GetLineFromLineNumber(lineNumber - 1); var actual = AutoIndent.GetLineIndentation(PythonTextBufferInfo.ForBuffer(null, buffer), line, view); Assert.AreEqual(expected, actual, line.GetText()); }
/// <summary> /// Write class to file. /// </summary> public void WriteCodeToFile() { // Format path to use proper directory separators. string filePath = this.outputDirectory .Replace('/', Path.DirectorySeparatorChar) .Replace('\\', Path.DirectorySeparatorChar) .TrimEnd(Path.DirectorySeparatorChar); filePath = string.Concat( Application.dataPath, Path.DirectorySeparatorChar, this.OutputDirectory, Path.DirectorySeparatorChar, this.className, ".cs"); // Try to write to file. try { // opens the file if it allready exists, creates it otherwise using (FileStream stream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write)) { using (StreamWriter writer = new StreamWriter(stream)) { StringBuilder builder = new StringBuilder(); AutoIndent indent = new AutoIndent(); // Header. builder.AppendLine("// ------------------------------------------------------------------------------") .AppendLine("// <auto-generated>") .AppendLine("// \tThis code was generated by a tool.") .AppendLine("//") .AppendLine("// \tChanges to this file may cause incorrect behavior and will be lost if") .AppendLine("// \tthe code is regenerated.") .AppendLine("// </auto-generated>") .AppendLine("// ------------------------------------------------------------------------------"); // Namespace begin. if (!string.IsNullOrEmpty(this.classNamespace)) { builder.AppendLine("namespace " + this.classNamespace) .AppendLine("{"); indent.IncreaseIndent(); } // Class begin. if (this.classDescription != null) { builder.AppendLine(indent + "/// <summary>") .AppendLine(indent + "/// " + this.classDescription) .AppendLine(indent + "/// </summary>"); } builder.AppendLine( string.Concat( indent, "public ", this.isStatic ? "static " : "", "class ", this.className )); builder.AppendLine(indent + "{"); indent.IncreaseIndent(); // Constants. foreach (KeyValuePair <string, object> entry in this.constants) { string type; if (entry.Value is string) { type = "string"; } else if (entry.Value is int) { type = "int"; } else if (entry.Value is double) { type = "double"; } else if (entry.Value is float) { type = "float"; } else { type = entry.Value.GetType().Name; } builder.AppendLine( String.Concat( indent, "public const ", type, " ", entry.Key, " = ", entry.Value is string? "\"" : "", entry.Value.ToString(), entry.Value is string? "\"" : "", ";")); } // Class end. indent.DecreaseIndent(); builder.AppendLine(indent + "}"); // Namespace end. if (!string.IsNullOrEmpty(this.classNamespace)) { indent.DecreaseIndent(); builder.AppendLine("}"); } writer.Write(builder.ToString()); } } } // Errors are generally a corrupt file. // Delete file if that happens, so it can be replaced. catch (Exception e) { Debug.LogException(e); if (File.Exists(filePath)) { File.Delete(filePath); } } AssetDatabase.Refresh(); }
/// <summary> /// Called from VS when we should handle a command or pass it on. /// </summary> public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { // preprocessing if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97) { switch ((VSConstants.VSStd97CmdID)nCmdID) { case VSConstants.VSStd97CmdID.GotoDefn: return(GotoDefinition()); case VSConstants.VSStd97CmdID.FindReferences: return(FindAllReferences()); } } else if (pguidCmdGroup == CommonConstants.Std2KCmdGroupGuid) { OutliningTaggerProvider.OutliningTagger tagger; switch ((VSConstants.VSStd2KCmdID)nCmdID) { case VSConstants.VSStd2KCmdID.RETURN: if (IronRubyToolsPackage.Instance.OptionsPage.AutoIndent) { AutoIndent.HandleReturn(_textView, (IClassifier)_textView.TextBuffer.Properties.GetProperty(typeof(IDlrClassifier))); return(VSConstants.S_OK); } break; #if FEATURE_INTELLISENSE case VSConstants.VSStd2KCmdID.SHOWMEMBERLIST: case VSConstants.VSStd2KCmdID.COMPLETEWORD: var controller = _textView.Properties.GetProperty <IntellisenseController>(typeof(IntellisenseController)); if (controller != null) { controller.TriggerCompletionSession((VSConstants.VSStd2KCmdID)nCmdID == VSConstants.VSStd2KCmdID.COMPLETEWORD); return(VSConstants.S_OK); } break; #endif case VSConstants.VSStd2KCmdID.OUTLN_STOP_HIDING_ALL: tagger = _textView.TextBuffer.GetOutliningTagger(); if (tagger != null) { tagger.Disable(); } // let VS get the event as well break; case VSConstants.VSStd2KCmdID.OUTLN_START_AUTOHIDING: tagger = _textView.TextBuffer.GetOutliningTagger(); if (tagger != null) { tagger.Enable(); } // let VS get the event as well break; case VSConstants.VSStd2KCmdID.COMMENT_BLOCK: case VSConstants.VSStd2KCmdID.COMMENTBLOCK: _textView.CommentBlock(); break; case VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK: case VSConstants.VSStd2KCmdID.UNCOMMENTBLOCK: _textView.UncommentBlock(); break; } } return(_next.Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut)); }
public int FormatSpan(IVsTextLines pBuffer, TextSpan[] ts) { MSXML.IXMLDOMNode codeNode, snippetTypes; int hr = VSConstants.S_OK; if (ErrorHandler.Failed(_session.GetSnippetNode("CodeSnippet:Code", out codeNode)) || codeNode == null) { return(hr); } if (ErrorHandler.Failed(_session.GetHeaderNode("CodeSnippet:SnippetTypes", out snippetTypes)) || snippetTypes == null) { return(hr); } bool surroundsWith = false; foreach (MSXML.IXMLDOMNode snippetType in snippetTypes.childNodes) { if (snippetType.nodeName == "SnippetType") { if (snippetType.text == SurroundsWith) { surroundsWith = true; break; } } } using (var edit = _textView.TextBuffer.CreateEdit()) { Span?endSpan = null; if (surroundsWith) { var templateText = codeNode.text.Replace("\n", _textView.Options.GetNewLineCharacter()); templateText = templateText.Replace("$end$", ""); // we can finally figure out where the selected text began witin the original template... int selectedIndex = templateText.IndexOf("$selected$"); if (selectedIndex != -1) { var start = _selectionStart.GetPosition(_textView.TextBuffer.CurrentSnapshot); var end = _selectionEnd.GetPosition(_textView.TextBuffer.CurrentSnapshot); if (end < start) { end = start; } endSpan = Span.FromBounds(start, end); } } _endCaretPoint = null; bool setCaretPoint = endSpan.HasValue && endSpan.Value.Start != endSpan.Value.End; if (setCaretPoint) { _endCaretPoint = _textView.TextBuffer.CurrentSnapshot.CreateTrackingPoint(endSpan.Value.End, PointTrackingMode.Positive); } // we now need to update any code which was not selected that we just inserted. AutoIndent.Format(_textView, edit, ts[0].iStartLine, ts[0].iEndLine, false, !setCaretPoint); edit.Apply(); } return(hr); }