本文整理汇总了C#中Mono.TextEditor.TextEditorData类的典型用法代码示例。如果您正苦于以下问题:C# TextEditorData类的具体用法?C# TextEditorData怎么用?C# TextEditorData使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TextEditorData类属于Mono.TextEditor命名空间,在下文中一共展示了TextEditorData类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InnerBlock
public static CommandRange InnerBlock(TextEditorData editor, char openingChar, char closingChar)
{
var range = Block(editor, openingChar, closingChar);
if (range.Length == 0)
return CommandRange.Empty;
int start = range.Start + 1;
int end = range.End - 2;
var line = editor.GetLine(editor.OffsetToLineNumber(range.Start));
// exclude newline if it comes just after opening char
if (line.EndOffsetIncludingDelimiter - start <= line.DelimiterLength)
start += line.DelimiterLength;
// exclude whitespace that comes just before the closing char...
line = editor.GetLine(editor.OffsetToLineNumber(range.End));
while (Char.IsWhiteSpace(editor.Text[end]) && end >= line.Offset)
end--;
//.. but only if newline comes after it
if (end >= line.Offset)
end = range.End - 2;
else
end -= line.PreviousLine.DelimiterLength;
if (start > end + 1)
return new CommandRange(start, start);
return new CommandRange(start, end+1);
}
示例2: CorrectIndenting
public override void CorrectIndenting (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain,
TextEditorData data, int line)
{
DocumentLine lineSegment = data.Document.GetLine (line);
if (lineSegment == null)
return;
try {
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
var tracker = new CSharpIndentEngine (data.Document, data.CreateNRefactoryTextEditorOptions (), policy.CreateOptions ());
tracker.Update (lineSegment.Offset);
for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++) {
tracker.Push (data.Document.GetCharAt (i));
}
string curIndent = lineSegment.GetIndentation (data.Document);
int nlwsp = curIndent.Length;
if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && data.Document.GetCharAt (lineSegment.Offset + nlwsp) == '*')) {
// Possibly replace the indent
string newIndent = tracker.ThisLineIndent;
if (newIndent != curIndent)
data.Replace (lineSegment.Offset, nlwsp, newIndent);
}
} catch (Exception e) {
LoggingService.LogError ("Error while indenting", e);
}
}
示例3: FormatText
/// <summary>
/// Used for formatting the entire document
/// </summary>
public override string FormatText(PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, string input, int startOffset, int endOffset)
{
var policy = policyParent.Get<DFormattingPolicy> (mimeTypeChain);
var textPolicy = policyParent.Get<TextStylePolicy> (mimeTypeChain);
var data = new TextEditorData{ Text = input };
if(IndentCorrectionOnly)
{
using(var s = data.OpenStream())
using(var r = new StreamReader(s))
D_Parser.Formatting.Indent.IndentEngineWrapper.CorrectIndent(r, startOffset, endOffset, data.Document.Replace, policy.Options, new TextStyleAdapter(textPolicy));
return data.Text;
}
var ast = DParser.ParseString(input, false, true) as DModule;
var formattingVisitor = new DFormattingVisitor(policy.Options, new DocAdapt(data.Document), ast, new TextStyleAdapter(textPolicy));
// Only clip to a region if it's necessary
if(startOffset > 0 || endOffset < input.Length-1)
{
formattingVisitor.CheckFormattingBoundaries = true;
var dl = data.Document.OffsetToLocation(startOffset);
formattingVisitor.FormattingStartLocation = new D_Parser.Dom.CodeLocation(dl.Column, dl.Line);
dl = data.Document.OffsetToLocation(endOffset);
formattingVisitor.FormattingEndLocation = new D_Parser.Dom.CodeLocation(dl.Column, dl.Line);
}
formattingVisitor.WalkThroughAst();
formattingVisitor.ApplyChanges(data.Document.Replace);
return data.Text;
}
示例4: Format
public static void Format (TextEditorData data, ProjectDom dom, DomLocation location, bool correctBlankLines)
{
PolicyContainer policyParent = dom != null && dom.Project != null? dom.Project.Policies
: PolicyService.DefaultPolicies;
var mimeTypeChain = DesktopService.GetMimeTypeInheritanceChain (CSharpFormatter.MimeType);
Format (policyParent, mimeTypeChain, data, dom, location, correctBlankLines);
}
示例5: RemoveCharBeforCaret
static void RemoveCharBeforCaret (TextEditorData data)
{
if (!data.IsSomethingSelected && MonoDevelop.Ide.Editor.DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket) {
if (data.Caret.Offset > 0) {
var line = data.GetLine (data.Caret.Line);
var stack = line.StartSpan.Clone();
if (stack.Any (s => s.Color == "string.other")) {
DeleteActions.Backspace (data);
return;
}
stack = line.StartSpan.Clone();
if (stack.Any (s => s.Color == "string.other")) {
DeleteActions.Backspace (data);
return;
}
char ch = data.Document.GetCharAt (data.Caret.Offset - 1);
int idx = open.IndexOf (ch);
if (idx >= 0) {
int nextCharOffset = GetNextNonWsCharOffset (data, data.Caret.Offset);
if (nextCharOffset >= 0 && closing[idx] == data.Document.GetCharAt (nextCharOffset)) {
data.Remove (data.Caret.Offset, nextCharOffset - data.Caret.Offset + 1);
}
}
}
}
DeleteActions.Backspace (data);
}
示例6: QuotedString
public static CommandRange QuotedString(TextEditorData editor, char quotationChar)
{
var start = editor.Caret.Offset;
var end = editor.Caret.Offset;
var lineOffset = editor.GetLine(editor.Caret.Line).Offset;
var lineEndOffset = editor.GetLine(editor.Caret.Line).EndOffset - 1; // Line includes \n
if (editor.Text[start] == quotationChar)
{
// Check if we're on closing char
start = lineOffset;
var openingCandidate = -1;
while (start < end)
{
if (editor.Text[start] == quotationChar & openingCandidate != -1)
openingCandidate = -1;
else if (editor.Text[start] == quotationChar)
openingCandidate = start;
start = start + 1;
}
if (openingCandidate != -1)
{
start = openingCandidate;
}
else
{
// not on closing char, let's find closing one
start = editor.Caret.Offset;
end = start + 1;
while (end < lineEndOffset & editor.Text[end] != quotationChar)
end++;
}
}
else
{
while (start >= lineOffset & editor.Text[start] != quotationChar)
start--;
while (end < lineEndOffset & editor.Text[end] != quotationChar)
end++;
}
if (start < 0 || end > lineEndOffset || start == end)
return CommandRange.Empty;
var endIncludingTrailingWhiteSpace = end;
var startIncludingTrailingWhiteSpace = start;
// expand to include all trailing white space
while (endIncludingTrailingWhiteSpace < lineEndOffset && Char.IsWhiteSpace(editor.Text[endIncludingTrailingWhiteSpace + 1]))
endIncludingTrailingWhiteSpace++;
// if there's no trailing white space then include leading
if (endIncludingTrailingWhiteSpace == end)
{
while (startIncludingTrailingWhiteSpace > lineOffset && Char.IsWhiteSpace(editor.Text[startIncludingTrailingWhiteSpace - 1]))
startIncludingTrailingWhiteSpace--;
}
return new CommandRange(Math.Min(start, startIncludingTrailingWhiteSpace), Math.Max(end, endIncludingTrailingWhiteSpace) + 1);
}
示例7: TestInsertionPoints
static void TestInsertionPoints (string text)
{
TextEditorData data = new TextEditorData ();
List<InsertionPoint> loc = new List<InsertionPoint> ();
for (int i = 0; i < text.Length; i++) {
char ch = text[i];
if (ch == '@') {
i++;
ch = text[i];
loc.Add (new InsertionPoint (data.Document.OffsetToLocation (data.Document.Length), ch == '3' || ch == '2', ch == '3' || ch == '1'));
} else {
data.Insert (data.Document.Length, ch.ToString ());
}
}
var parseResult = new NRefactoryParser ().Parse (null, "a.cs", data.Document.Text);
var foundPoints = HelperMethods.GetInsertionPoints (data.Document, parseResult.CompilationUnit.Types[0]);
Assert.AreEqual (loc.Count, foundPoints.Count, "point count doesn't match");
for (int i = 0; i < loc.Count; i++) {
Console.WriteLine (loc[i] + "/" + foundPoints[i]);
Assert.AreEqual (loc[i].Location, foundPoints[i].Location, "point " + i + " doesn't match");
Assert.AreEqual (loc[i].ShouldInsertNewLineAfter, foundPoints[i].ShouldInsertNewLineAfter, "point " + i + " ShouldInsertNewLineAfter doesn't match");
Assert.AreEqual (loc[i].ShouldInsertNewLineBefore, foundPoints[i].ShouldInsertNewLineBefore, "point " + i + " ShouldInsertNewLineBefore doesn't match");
}
}
示例8: RemoveCharBeforCaret
static void RemoveCharBeforCaret (TextEditorData data)
{
if (((ISourceEditorOptions)data.Options).AutoInsertMatchingBracket) {
if (data.Caret.Offset > 0) {
var line = data.GetLine (data.Caret.Line);
var stack = line.StartSpan.Clone();
if (stack.Any (s => s.Color == "string.other")) {
DeleteActions.Backspace (data);
return;
}
stack = line.StartSpan.Clone();
if (stack.Any (s => s.Color == "string.other")) {
DeleteActions.Backspace (data);
return;
}
char ch = data.Document.GetCharAt (data.Caret.Offset - 1);
int idx = open.IndexOf (ch);
if (idx >= 0) {
int nextCharOffset = GetNextNonWsCharOffset (data, data.Caret.Offset);
if (nextCharOffset >= 0 && closing[idx] == data.Document.GetCharAt (nextCharOffset)) {
bool updateToEnd = data.Document.OffsetToLineNumber (nextCharOffset) != data.Caret.Line;
data.Remove (data.Caret.Offset, nextCharOffset - data.Caret.Offset + 1);
}
}
}
}
DeleteActions.Backspace (data);
}
示例9: TestNamespaceBraceStyle
public void TestNamespaceBraceStyle ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text = @"namespace A
{
namespace B {
class Test {}
}
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.NamespaceBraceStyle = BraceStyle.EndOfLine;
policy.ClassBraceStyle = BraceStyle.DoNotChange;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"namespace A {
namespace B {
class Test {}
}
}", data.Document.Text);
policy.NamespaceBraceStyle = BraceStyle.NextLineShifted;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"namespace A
{
namespace B
{
class Test {}
}
}", data.Document.Text);
}
示例10:
List<ReferenceSegment> IAssemblyBrowserNodeBuilder.Disassemble (TextEditorData data, ITreeNavigator navigator)
{
if (DomMethodNodeBuilder.HandleSourceCodeEntity (navigator, data))
return null;
var field = CecilLoader.GetCecilObject ((IUnresolvedField)navigator.DataItem);
return DomMethodNodeBuilder.Disassemble (data, rd => rd.DisassembleField (field));
}
示例11: LoadMark
public void LoadMark(TextEditorData data)
{
// remember where the caret currently is
int lineNumber = data.Caret.Line;
int colNumber = data.Caret.Column;
// get the line number stored with this mark
int x = (base.LineSegment == null)? 1 : base.LineSegment.LineNumber;
// reposition the caret on the stored line
data.Caret.Line = x;
int len = base.LineSegment.LengthIncludingDelimiter;
if (ColumnNumber >= len) {
// Check if the line has been truncated after the setting the mark
data.Caret.Column = len - 1;
} else {
data.Caret.Column = ColumnNumber;
}
if (MarkCharacter == '`') {
data.Document.RemoveMarker (this);
base.LineSegment = data.Document.GetLine(lineNumber);
ColumnNumber = colNumber;
Console.WriteLine ("Line before jump: {0}", lineNumber);
data.Document.AddMarker (lineNumber, this);
data.Document.RequestUpdate (new UpdateAll ());
data.Document.CommitDocumentUpdate ();
}
}
示例12: TestIndentClassBody
public void TestIndentClassBody ()
{
TextEditorData data = new TextEditorData ();
data.Document.FileName = "a.cs";
data.Document.Text =
@"class Test
{
Test a;
}";
CSharpFormattingPolicy policy = new CSharpFormattingPolicy ();
policy.IndentClassBody = true;
CSharp.Dom.CompilationUnit compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
{
Test a;
}", data.Document.Text);
policy.IndentClassBody = false;
compilationUnit = new CSharpParser ().Parse (data);
compilationUnit.AcceptVisitor (new DomIndentationVisitor (policy, data), null);
Assert.AreEqual (@"class Test
{
Test a;
}", data.Document.Text);
}
示例13: GetResult
public GenerateNamespaceImport GetResult (ProjectDom dom, ICompilationUnit unit, IType type, TextEditorData data)
{
GenerateNamespaceImport result;
if (cache.TryGetValue (type.Namespace, out result))
return result;
result = new GenerateNamespaceImport ();
cache[type.Namespace] = result;
result.InsertNamespace = false;
DomLocation location = new DomLocation (data.Caret.Line, data.Caret.Column);
foreach (IUsing u in unit.Usings.Where (u => u.ValidRegion.Contains (location))) {
if (u.Namespaces.Any (ns => type.Namespace == ns)) {
result.GenerateUsing = false;
return result;
}
}
result.GenerateUsing = true;
string name = type.DecoratedFullName.Substring (type.Namespace.Length + 1);
foreach (IUsing u in unit.Usings.Where (u => u.ValidRegion.Contains (location))) {
foreach (string ns in u.Namespaces) {
if (dom.SearchType (unit, unit.GetTypeAt (location), unit.GetMemberAt (location), ns + "." + name) != null) {
result.GenerateUsing = false;
result.InsertNamespace = true;
return result;
}
}
}
return result;
}
示例14: InnerQuotedString
public static CommandRange InnerQuotedString(TextEditorData editor, char enclosingChar)
{
var range = QuotedString(editor, enclosingChar);
if (range.Length == 0)
return CommandRange.Empty;
return new CommandRange(range.Start + 1, range.End - 1);
}
示例15: FormatText
public string FormatText (CSharpFormattingPolicy policy, TextStylePolicy textPolicy, string mimeType, string input, int startOffset, int endOffset)
{
var data = new TextEditorData ();
data.Document.SuppressHighlightUpdate = true;
data.Document.MimeType = mimeType;
data.Document.FileName = "toformat.cs";
if (textPolicy != null) {
data.Options.TabsToSpaces = textPolicy.TabsToSpaces;
data.Options.TabSize = textPolicy.TabWidth;
data.Options.IndentationSize = textPolicy.IndentWidth;
data.Options.IndentStyle = textPolicy.RemoveTrailingWhitespace ? IndentStyle.Virtual : IndentStyle.Smart;
}
data.Text = input;
// System.Console.WriteLine ("-----");
// System.Console.WriteLine (data.Text.Replace (" ", ".").Replace ("\t", "->"));
// System.Console.WriteLine ("-----");
var parser = new CSharpParser ();
var compilationUnit = parser.Parse (data);
bool hadErrors = parser.HasErrors;
if (hadErrors) {
// foreach (var e in parser.ErrorReportPrinter.Errors)
// Console.WriteLine (e.Message);
return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset));
}
var originalVersion = data.Document.Version;
var textEditorOptions = data.CreateNRefactoryTextEditorOptions ();
var formattingVisitor = new ICSharpCode.NRefactory.CSharp.CSharpFormatter (
policy.CreateOptions (),
textEditorOptions
) {
FormattingMode = FormattingMode.Intrusive
};
var changes = formattingVisitor.AnalyzeFormatting (data.Document, compilationUnit);
try {
changes.ApplyChanges (startOffset, endOffset - startOffset);
} catch (Exception e) {
LoggingService.LogError ("Error in code formatter", e);
return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset));
}
// check if the formatter has produced errors
parser = new CSharpParser ();
parser.Parse (data);
if (parser.HasErrors) {
LoggingService.LogError ("C# formatter produced source code errors. See console for output.");
return input.Substring (startOffset, Math.Max (0, Math.Min (endOffset, input.Length) - startOffset));
}
var currentVersion = data.Document.Version;
string result = data.GetTextBetween (startOffset, originalVersion.MoveOffsetTo (currentVersion, endOffset, ICSharpCode.NRefactory.Editor.AnchorMovementType.Default));
data.Dispose ();
return result;
}