本文整理汇总了C#中PolicyContainer.Get方法的典型用法代码示例。如果您正苦于以下问题:C# PolicyContainer.Get方法的具体用法?C# PolicyContainer.Get怎么用?C# PolicyContainer.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PolicyContainer
的用法示例。
在下文中一共展示了PolicyContainer.Get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例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: CorrectIndenting
public override void CorrectIndenting (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain,
TextEditorData data, int line)
{
DocumentLine lineSegment = data.Document.GetLine (line);
if (lineSegment == null)
return;
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
var textPolicy = policyParent.Get<TextStylePolicy> (mimeTypeChain);
var tracker = new DocumentStateTracker<CSharpIndentEngine> (new CSharpIndentEngine (policy, textPolicy), data);
tracker.UpdateEngine (lineSegment.Offset);
for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++) {
tracker.Engine.Push (data.Document.GetCharAt (i));
}
string curIndent = lineSegment.GetIndentation (data.Document);
int nlwsp = curIndent.Length;
if (!tracker.Engine.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && data.Document.GetCharAt (lineSegment.Offset + nlwsp) == '*')) {
// Possibly replace the indent
string newIndent = tracker.Engine.ThisLineIndent;
if (newIndent != curIndent)
data.Replace (lineSegment.Offset, nlwsp, newIndent);
}
tracker.Dispose ();
}
示例4: CorrectIndentingImplementation
protected override void CorrectIndentingImplementation (PolicyContainer policyParent, TextEditor editor, int line)
{
var lineSegment = editor.GetLine (line);
if (lineSegment == null)
return;
try {
var policy = policyParent.Get<CSharpFormattingPolicy> (MimeType);
var textpolicy = policyParent.Get<TextStylePolicy> (MimeType);
var tracker = new CSharpIndentEngine (policy.CreateOptions (textpolicy));
tracker.Update (IdeApp.Workbench.ActiveDocument.Editor, lineSegment.Offset);
for (int i = lineSegment.Offset; i < lineSegment.Offset + lineSegment.Length; i++) {
tracker.Push (editor.GetCharAt (i));
}
string curIndent = lineSegment.GetIndentation (editor);
int nlwsp = curIndent.Length;
if (!tracker.LineBeganInsideMultiLineComment || (nlwsp < lineSegment.LengthIncludingDelimiter && editor.GetCharAt (lineSegment.Offset + nlwsp) == '*')) {
// Possibly replace the indent
string newIndent = tracker.ThisLineIndent;
if (newIndent != curIndent)
editor.ReplaceText (lineSegment.Offset, nlwsp, newIndent);
}
} catch (Exception e) {
LoggingService.LogError ("Error while indenting", e);
}
}
示例5: FormatImplementation
protected override Core.Text.ITextSource FormatImplementation (PolicyContainer policyParent, string mimeType, Core.Text.ITextSource input, int startOffset, int length)
{
if (policyParent == null)
policyParent = PolicyService.DefaultPolicies;
var txtPol = policyParent.Get<TextStylePolicy> (mimeType);
var xmlPol = policyParent.Get<XmlFormattingPolicy> (mimeType);
return new StringTextSource(FormatXml (txtPol, xmlPol, input.Text));
}
示例6: FormatText
public string FormatText (PolicyContainer policyParent, string mimeType, string input)
{
TextStylePolicy policy;
if (policyParent != null)
policy = policyParent.Get <TextStylePolicy> (DesktopService.GetMimeTypeInheritanceChain (mimeType));
else
policy = PolicyService.GetDefaultPolicy <TextStylePolicy> (DesktopService.GetMimeTypeInheritanceChain (mimeType));
XmlTextReader reader = new XmlTextReader (new StringReader (input));
reader.WhitespaceHandling = WhitespaceHandling.None;
StringWriter indentedXmlWriter = new StringWriter ();
XmlWriterSettings settings = new XmlWriterSettings ();
settings.Indent = true;
if (policy.TabsToSpaces) {
settings.IndentChars = new string (' ', policy.TabWidth);
} else {
settings.IndentChars = "\t";
}
settings.NewLineChars = policy.GetEolMarker ();
try {
XmlWriter xmlWriter = XmlTextWriter.Create (indentedXmlWriter, settings);
xmlWriter.WriteNode (reader, false);
xmlWriter.Flush ();
} catch {
// Ignore malfored xml
return input;
}
return indentedXmlWriter.ToString ();
}
示例7: Format
public static void Format (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain,
TextEditorData data, ProjectDom dom, DomLocation location, bool correctBlankLines)
{
var compilationUnit = new MonoDevelop.CSharp.Parser.CSharpParser ().Parse (data);
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
var domSpacingVisitor = new DomSpacingVisitor (policy, data) {
AutoAcceptChanges = false,
};
compilationUnit.AcceptVisitor (domSpacingVisitor, null);
var domIndentationVisitor = new DomIndentationVisitor (policy, data) {
AutoAcceptChanges = false,
};
domIndentationVisitor.CorrectBlankLines = correctBlankLines;
compilationUnit.AcceptVisitor (domIndentationVisitor, null);
var changes = new List<Change> ();
changes.AddRange (domSpacingVisitor.Changes);
changes.AddRange (domIndentationVisitor.Changes);
RefactoringService.AcceptChanges (null, null, changes);
}
示例8: InternalFormat
protected override string InternalFormat (PolicyContainer policyParent, string mimeType, string input, int startOffset, int endOffset)
{
IEnumerable<string> mtypes = DesktopService.GetMimeTypeInheritanceChain (mimeType);
TextStylePolicy currentPolicy = policyParent != null
? policyParent.Get<TextStylePolicy> (mtypes)
: MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<TextStylePolicy> (mtypes);
input = input ?? "";
int line = 0, col = 0;
string eolMarker = currentPolicy.GetEolMarker ();
StringBuilder result = new StringBuilder ();
for (int i = startOffset; i <= endOffset; i++) {
char ch = input[i];
switch (ch) {
case '\t':
if (currentPolicy.TabsToSpaces) {
int tabWidth = GetNextTabstop (col, currentPolicy.TabWidth) - col;
result.Append (new string (' ', tabWidth));
col += tabWidth;
} else
goto default;
break;
case '\r':
if (i + 1 < input.Length && input[i + 1] == '\n')
i++;
goto case '\n';
case '\n':
result.Append (eolMarker);
line++;
col = 0;
break;
default:
result.Append (ch);
col++;
break;
}
}
return result.ToString ();
}
示例9: FormatText
public override string FormatText (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain,
string input, int startOffset, int endOffset)
{
var currentPolicy = policyParent.Get<TextStylePolicy> (mimeTypeChain);
input = input ?? "";
int line = 0, col = 0;
string eolMarker = currentPolicy.GetEolMarker ();
var result = new StringBuilder ();
for (int i = startOffset; i <= endOffset && i < input.Length; i++) {
char ch = input[i];
switch (ch) {
case '\t':
if (currentPolicy.TabsToSpaces) {
int tabWidth = GetNextTabstop (col, currentPolicy.TabWidth) - col;
result.Append (new string (' ', tabWidth));
col += tabWidth;
} else
goto default;
break;
case '\r':
if (i + 1 < input.Length && input[i + 1] == '\n')
i++;
goto case '\n';
case '\n':
result.Append (eolMarker);
line++;
col = 0;
break;
default:
result.Append (ch);
col++;
break;
}
}
return result.ToString ();
}
示例10: FormatImplementation
protected override ITextSource FormatImplementation (PolicyContainer policyParent, string mimeType, ITextSource input, int startOffset, int length)
{
var currentPolicy = policyParent.Get<TextStylePolicy> (mimeType);
int line = 0, col = 0;
string eolMarker = currentPolicy.GetEolMarker ();
var result = new StringBuilder ();
var endOffset = startOffset + length;
for (int i = startOffset; i < endOffset && i < input.Length; i++) {
char ch = input[i];
switch (ch) {
case '\t':
if (currentPolicy.TabsToSpaces) {
int tabWidth = GetNextTabstop (col, currentPolicy.TabWidth) - col;
result.Append (new string (' ', tabWidth));
col += tabWidth;
} else
goto default;
break;
case '\r':
if (i + 1 < input.Length && input[i + 1] == '\n')
i++;
goto case '\n';
case '\n':
result.Append (eolMarker);
line++;
col = 0;
break;
default:
result.Append (ch);
col++;
break;
}
}
return new StringTextSource (result.ToString (), input.Encoding, input.UseBOM);
}
示例11: GetFormattingChanges
static AstFormattingVisitor GetFormattingChanges (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, MonoDevelop.Ide.Gui.Document document, string input)
{
using (var stubData = TextEditorData.CreateImmutable (input)) {
stubData.Document.FileName = document.FileName;
var parser = document.HasProject ? new ICSharpCode.NRefactory.CSharp.CSharpParser (TypeSystemParser.GetCompilerArguments (document.Project)) : new ICSharpCode.NRefactory.CSharp.CSharpParser ();
var compilationUnit = parser.Parse (stubData);
bool hadErrors = parser.HasErrors;
// try it out, if the behavior is better when working only with correct code.
if (hadErrors) {
return null;
}
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
var formattingVisitor = new AstFormattingVisitor (policy.CreateOptions (), stubData.Document, document.Editor.CreateNRefactoryTextEditorOptions ()) {
HadErrors = hadErrors
};
compilationUnit.AcceptVisitor (formattingVisitor);
return formattingVisitor;
}
}
示例12: GetFormattingChanges
static FormattingChanges GetFormattingChanges (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, MonoDevelop.Ide.Gui.Document document, string input, DomRegion formattingRegion, ref int formatStartOffset, ref int formatLength, bool formatLastStatementOnly)
{
using (var stubData = TextEditorData.CreateImmutable (input)) {
stubData.Document.FileName = document.FileName;
var parser = document.HasProject ? new CSharpParser (TypeSystemParser.GetCompilerArguments (document.Project)) : new CSharpParser ();
var compilationUnit = parser.Parse (stubData);
bool hadErrors = parser.HasErrors;
if (hadErrors) {
using (var stubData2 = TextEditorData.CreateImmutable (input + "}")) {
compilationUnit = parser.Parse (stubData2);
hadErrors = parser.HasErrors;
}
}
// try it out, if the behavior is better when working only with correct code.
if (hadErrors) {
return null;
}
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
var formattingVisitor = new ICSharpCode.NRefactory.CSharp.CSharpFormatter (policy.CreateOptions (), document.Editor.CreateNRefactoryTextEditorOptions ());
formattingVisitor.FormattingMode = FormattingMode.Intrusive;
formattingVisitor.AddFormattingRegion (formattingRegion);
var changes = formattingVisitor.AnalyzeFormatting (stubData.Document, compilationUnit);
if (formatLastStatementOnly) {
AstNode node = compilationUnit.GetAdjacentNodeAt<Statement> (stubData.OffsetToLocation (formatStartOffset + formatLength - 1));
if (node != null) {
while (node.Role == Roles.EmbeddedStatement || node.Role == IfElseStatement.TrueRole || node.Role == IfElseStatement.FalseRole)
node = node.Parent;
// include indentation if node starts in new line
var formatNode = node.GetPrevNode ();
if (formatNode.Role != Roles.NewLine)
formatNode = node;
var start = stubData.LocationToOffset (formatNode.StartLocation);
if (start > formatStartOffset) {
var end = stubData.LocationToOffset (node.EndLocation);
formatStartOffset = start;
formatLength = end - start;
}
}
}
return changes;
}
}
示例13: FormatText
public string FormatText (PolicyContainer policyParent, IEnumerable<string> mimeTypeInheritanceChain, string input)
{
var txtPol = policyParent.Get<TextStylePolicy> (mimeTypeInheritanceChain);
var xmlPol = policyParent.Get<XmlFormattingPolicy> (mimeTypeInheritanceChain);
return FormatXml (txtPol, xmlPol, input);
}
示例14: Format
public static void Format (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, MonoDevelop.Ide.Gui.Document data, ProjectDom dom, DomLocation location, bool correctBlankLines, bool runAferCR = false)
{
if (data.ParsedDocument == null || data.ParsedDocument.CompilationUnit == null)
return;
var member = data.ParsedDocument.CompilationUnit.GetMemberAt (location.Line + (runAferCR ? -1 : 0), location.Column);
if (member == null || member.Location.IsEmpty || member.BodyRegion.End.IsEmpty)
return;
StringBuilder sb = new StringBuilder ();
int closingBrackets = 0;
DomRegion validRegion = DomRegion.Empty;
foreach (var u in data.ParsedDocument.CompilationUnit.Usings.Where (us => us.IsFromNamespace)) {
// the dom parser breaks A.B.C into 3 namespaces with the same region, this is filtered here
if (u.ValidRegion == validRegion)
continue;
validRegion = u.ValidRegion;
sb.Append ("namespace Stub {");
closingBrackets++;
}
var parent = member.DeclaringType;
while (parent != null) {
sb.Append ("class Stub {");
closingBrackets++;
parent = parent.DeclaringType;
}
sb.AppendLine ();
int startOffset = sb.Length;
sb.Append (data.Editor.GetTextBetween (member.Location.Line, 1, member.BodyRegion.End.Line + (runAferCR ? 1 : 0), member.BodyRegion.End.Column));
int endOffset = sb.Length;
sb.AppendLine ();
sb.Append (new string ('}', closingBrackets));
TextEditorData stubData = new TextEditorData () { Text = sb.ToString () };
stubData.Document.FileName = data.FileName;
var compilationUnit = new MonoDevelop.CSharp.Parser.CSharpParser ().Parse (stubData);
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
var domSpacingVisitor = new AstSpacingVisitor (policy, stubData) {
AutoAcceptChanges = false,
};
compilationUnit.AcceptVisitor (domSpacingVisitor, null);
var domIndentationVisitor = new AstIndentationVisitor (policy, stubData) {
AutoAcceptChanges = false,
};
domIndentationVisitor.CorrectBlankLines = correctBlankLines;
compilationUnit.AcceptVisitor (domIndentationVisitor, null);
var changes = new List<Change> ();
changes.AddRange (domSpacingVisitor.Changes.Cast<TextReplaceChange> ().Where (c => startOffset < c.Offset && c.Offset < endOffset));
changes.AddRange (domIndentationVisitor.Changes.Cast<TextReplaceChange> ().Where (c => startOffset < c.Offset && c.Offset < endOffset));
int delta = data.Editor.LocationToOffset (member.Location.Line, 1) - startOffset;
HashSet<int> lines = new HashSet<int> ();
foreach (TextReplaceChange change in changes) {
if (change is AstSpacingVisitor.MyTextReplaceChange)
((AstSpacingVisitor.MyTextReplaceChange)change).SetTextEditorData (data.Editor);
change.Offset += delta;
lines.Add (data.Editor.OffsetToLineNumber (change.Offset));
}
RefactoringService.AcceptChanges (null, null, changes);
foreach (int line in lines)
data.Editor.Document.CommitLineUpdate (line);
stubData.Dispose ();
}
示例15: FormatImplementation
protected override ITextSource FormatImplementation (PolicyContainer policyParent, string mimeType, ITextSource input, int startOffset, int length)
{
var policy = policyParent.Get<CSharpFormattingPolicy> (mimeType);
var textPolicy = policyParent.Get<TextStylePolicy> (mimeType);
return new StringTextSource (FormatText (policy, textPolicy, input.Text, startOffset, startOffset + length));
}