T GetInheritedPolicy(IEnumerable <string> scopes) { foreach (string scope in scopes) { PolicyContainer currentBag = scope == mimeType ? policyContainer.ParentPolicies : policyContainer; while (currentBag != null) { if (currentBag.DirectHas <T> (scope)) { T pol = currentBag.DirectGet <T> (scope); if (pol != null) { return(pol); } // Default settings requested for this scope. Start looking from the original // bag now using the next scope in the chain break; } else { currentBag = currentBag.ParentPolicies; } } } return(PolicyService.GetDefaultPolicy <T>(scopes)); }
public static async Task <OptionSet> GetOptionsAsync(this DocumentContext ctx, CancellationToken cancellationToken = default(CancellationToken)) { if (ctx == null) { throw new ArgumentNullException(nameof(ctx)); } try { if (ctx.AnalysisDocument != null) { var result = await ctx.AnalysisDocument.GetOptionsAsync().ConfigureAwait(false); if (result != null) { return(result); } } var policies = ctx.Project?.Policies; if (policies == null) { var defaultPolicy = PolicyService.GetDefaultPolicy <CSharpFormattingPolicy> (CSharpFormatter.MimeType); var defaultTextPolicy = PolicyService.GetDefaultPolicy <TextStylePolicy> (CSharpFormatter.MimeType); return(defaultPolicy.CreateOptions(defaultTextPolicy)); } var policy = policies.Get <CSharpFormattingPolicy> (CSharpFormatter.MimeType); var textpolicy = policies.Get <TextStylePolicy> (CSharpFormatter.MimeType); return(policy.CreateOptions(textpolicy)); } catch (Exception e) { LoggingService.LogError("Error while getting document options", e); return(ctx.GetOptionSet()); } }
public static TextStylePolicy GetTextStylePolicy(this OptionKey optionKey) { var mimeChain = DesktopService.GetMimeTypeInheritanceChainForRoslynLanguage(optionKey.Language); if (mimeChain == null) { throw new Exception($"Unknown Roslyn language {optionKey.Language}"); } return(PolicyService.GetDefaultPolicy <TextStylePolicy> (mimeChain)); }
TextStylePolicy GetTextPolicyForLanguage(string language) { var mimeChain = DesktopService.GetMimeTypeInheritanceChainForRoslynLanguage(language); if (mimeChain == null) { throw new Exception($"Unknown Roslyn language {language}"); } return(PolicyService.GetDefaultPolicy <TextStylePolicy> (mimeChain)); }
T GetDefaultValue() { if (defaultPolicyContainer != null) { return(defaultPolicyContainer.Get <T> ()); } else { return(PolicyService.GetDefaultPolicy <T> ()); } }
// CorrectIndenting is completely unused in the entire MonoDevelopment code environment - doesn't have to be implemented /// <summary> /// Used for formatting selected code /// </summary> public override void OnTheFlyFormat(Ide.Gui.Document _doc, int startOffset, int endOffset) { var doc = _doc.Editor.Document; DFormattingPolicy policy = null; TextStylePolicy textStyle = null; if (_doc.HasProject) { policy = _doc.Project.Policies.Get <DFormattingPolicy>(Indentation.DTextEditorIndentation.mimeTypes); textStyle = _doc.Project.Policies.Get <TextStylePolicy>(Indentation.DTextEditorIndentation.mimeTypes); } else { policy = PolicyService.GetDefaultPolicy <DFormattingPolicy> (Indentation.DTextEditorIndentation.mimeTypes); textStyle = PolicyService.GetDefaultPolicy <TextStylePolicy> (Indentation.DTextEditorIndentation.mimeTypes); } if (IndentCorrectionOnly) { using (doc.OpenUndoGroup()) using (var r = doc.CreateReader()) D_Parser.Formatting.Indent.IndentEngineWrapper.CorrectIndent(r, startOffset, endOffset, doc.Replace, policy.Options, new TextStyleAdapter(textStyle) { KeepAlignmentSpaces = policy.KeepAlignmentSpaces }); return; } var dpd = _doc.ParsedDocument as ParsedDModule; if (dpd == null) { return; } var formattingVisitor = new DFormattingVisitor(policy.Options, new DocAdapt(doc), dpd.DDom, new TextStyleAdapter(textStyle)); formattingVisitor.CheckFormattingBoundaries = true; var dl = doc.OffsetToLocation(startOffset); formattingVisitor.FormattingStartLocation = new CodeLocation(dl.Column, dl.Line); dl = doc.OffsetToLocation(endOffset); formattingVisitor.FormattingEndLocation = new CodeLocation(dl.Column, dl.Line); formattingVisitor.WalkThroughAst(); using (doc.OpenUndoGroup()) formattingVisitor.ApplyChanges(doc.Replace); }
private Policy GetLocalPolicy(PolicyParam policyParam, string policyId, string innerPlatformCode, UserRelation userRealtion, bool IspolicyIsNull, PolicyService policyService, Policy localPolicy) { PnrData pnrData = PnrHelper.GetPnrData(order.PnrContent); if (IspolicyIsNull) { //全部查询匹配一下 localPolicy = policyService.GetLocalPolicy(userRealtion, innerPlatformCode, policyParam, pnrData).Find((p) => p.PolicyId == policyId); if (localPolicy == null) { localPolicy = policyService.GetSharePolicy(userRealtion, innerPlatformCode, policyParam, pnrData).Find((p) => p.PolicyId == policyId); } if (localPolicy == null && policyId.StartsWith(userRealtion.carrier.Code + "_")) { //本地 默认 localPolicy = policyService.GetDefaultPolicy(userRealtion, innerPlatformCode, policyParam, pnrData, policyId).FirstOrDefault(); } } else { //非接口重新获取政策 //本地 localPolicy = policyService.GetLocalPolicy(userRealtion, innerPlatformCode, policyParam, pnrData).Find((p) => p.PolicyId == policyId); if (localPolicy == null) { //共享 异地 localPolicy = policyService.GetSharePolicy(userRealtion, innerPlatformCode, policyParam, pnrData).Find((p) => p.PolicyId == policyId); } if (localPolicy == null) { if (policyId.StartsWith(userRealtion.carrier.Code + "_")) { //本地 默认 localPolicy = policyService.GetDefaultPolicy(userRealtion, innerPlatformCode, policyParam, pnrData, policyId).FirstOrDefault(); } } } return(localPolicy); }
public static CommitMessageFormat GetCommitMessageFormat(ChangeSet cset, out AuthorInformation authorInfo) { // If all files belong to a project, use that project's policy. If not, use the solution policy Project project = null; bool sameProject = true; foreach (ChangeSetItem item in cset.Items) { if (project != null) { if (project.Files.GetFile(item.LocalPath) == null) { // Not all files belong to the same project sameProject = false; break; } } else { project = IdeApp.Workspace.GetProjectsContainingFile(item.LocalPath).FirstOrDefault(); } } CommitMessageStyle style; if (project != null) { VersionControlPolicy policy; if (sameProject) { policy = project.Policies.Get <VersionControlPolicy> (); } else { policy = project.ParentSolution.Policies.Get <VersionControlPolicy> (); } style = policy.CommitMessageStyle; } else { style = PolicyService.GetDefaultPolicy <CommitMessageStyle> (); } authorInfo = project != null ? project.AuthorInformation : AuthorInformation.Default; CommitMessageFormat format = new CommitMessageFormat(); format.Style = style; format.ShowFilesForSingleComment = false; return(format); }
public override Stream CreateFileContent(SolutionFolderItem policyParent, Project project, string language, string fileName, string identifier) { if (Outer.FormatCode) { return(base.CreateFileContent(policyParent, project, language, fileName, identifier)); } var model = GetTagModel(policyParent, project, language, identifier, fileName); string text = CreateContent(project, model.OverrideTags, language); text = ProcessContent(text, model); var memoryStream = new MemoryStream(); byte[] preamble = Encoding.UTF8.GetPreamble(); memoryStream.Write(preamble, 0, preamble.Length); if (AddStandardHeader) { string header = StandardHeaderService.GetHeader(policyParent, fileName, true); byte[] bytes = Encoding.UTF8.GetBytes(header); memoryStream.Write(bytes, 0, bytes.Length); } var textDocument = TextEditorFactory.CreateNewDocument(); //var textDocument = new TextDocument (); textDocument.Text = text; var textStylePolicy = (policyParent == null) ? PolicyService.GetDefaultPolicy <TextStylePolicy> ("text/plain") : policyParent.Policies.Get <TextStylePolicy> ("text/plain"); string eolMarker = TextStylePolicy.GetEolMarker(textStylePolicy.EolMarker); byte[] eol = Encoding.UTF8.GetBytes(eolMarker); string indent = (!textStylePolicy.TabsToSpaces) ? null : new string (' ', textStylePolicy.TabWidth); foreach (var current in textDocument.GetLines()) { string line = textDocument.GetTextAt(current.Offset, current.Length); if (indent != null) { line = line.Replace(" ", indent); } byte[] bytes = Encoding.UTF8.GetBytes(line); memoryStream.Write(bytes, 0, bytes.Length); memoryStream.Write(eol, 0, eol.Length); } memoryStream.Position = 0; return(memoryStream); }
T GetCurrentPolicy() { object pol = null; if (loaded) { pol = GetPolicy(); } else if (hasCachedPolicy) { pol = cachedPolicy; } if (pol == null) { pol = policyContainer.Get <T> (mimeTypeScopes) ?? PolicyService.GetDefaultPolicy <T> (mimeTypeScopes); } return((T)pol); }
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()); }
void IMimeTypePolicyOptionsPanel.LoadSetPolicy(PolicyContainer pset) { object selected = pset.Get <T> (mimeTypeScopes); if (selected == null) { selected = PolicyService.GetDefaultPolicy <T> (mimeTypeScopes); } if (loaded) { if (defaultSettingsButton != null) { defaultSettingsButton.Active = false; panelWidget.Sensitive = true; } LoadFrom((T)selected); } else { cachedPolicy = selected; hasCachedPolicy = true; } }
public override bool KeyPress(Key key, char keyChar, ModifierType modifier) { var ed = Document.Editor; var dPolicy = Document.HasProject ? Document.Project.Policies.Get <DFormattingPolicy> ("text/x-d") : PolicyService.GetDefaultPolicy <DFormattingPolicy> ("text/x-d"); if (key == Key.Return) { ed.DeleteSelectedText(true); int lastBegin; int lastEnd; var caretCtxt = CaretContextAnalyzer.GetTokenContext(ed.Text, ed.Caret.Offset, out lastBegin, out lastEnd); if (lastBegin >= 0 && (caretCtxt == TokenContext.BlockComment || caretCtxt == TokenContext.NestedComment)) { var charsToInsert = " " + (caretCtxt == TokenContext.BlockComment ? '*' : '+') + " "; var commentBeginIndent = ed.GetLineIndent(ed.GetLineByOffset(lastBegin)); ed.InsertAtCaret( Document.Editor.EolMarker + commentBeginIndent + (dPolicy.InsertStarAtCommentNewLine ? charsToInsert : "")); return(false); } } if (Document.Editor.Options.IndentStyle == IndentStyle.Smart || Document.Editor.Options.IndentStyle == IndentStyle.Virtual) { int newIndentation = 0; if (key == Key.Return) { ed.InsertAtCaret(Document.Editor.EolMarker); var tr = ed.Document.CreateReader(); var cb = DCodeFormatter.NativeFormatterInstance.CalculateIndentation(tr, ed.Caret.Line); tr.Close(); newIndentation = cb == null ? 0 : cb.GetLineIndentation(ed.Caret.Line); ed.InsertAtCaret(CalculateIndentationString(newIndentation)); return(false); } else if (keyChar == '{' || keyChar == '}' || keyChar == ':') { ed.DeleteSelectedText(true); ed.InsertAtCaret(keyChar.ToString()); // Ensure that we aren't in a non-code area right now - automatically trying to format comments is awful int lastBegin; int lastEnd; var caretCtxt = CaretContextAnalyzer.GetTokenContext(ed.Text, ed.Caret.Offset, out lastBegin, out lastEnd); if (lastBegin >= 0 && caretCtxt != TokenContext.None) { return(false); } var origInd = ed.GetLineIndent(ed.Caret.Line); int originalIndentation = origInd.Length; var tr = ed.Document.CreateReader(); var cb = DCodeFormatter.NativeFormatterInstance.CalculateIndentation(tr, ed.Caret.Line); tr.Close(); newIndentation = cb == null ? 0 : cb.GetLineIndentation(ed.Caret.Line); var newInd = CalculateIndentationString(newIndentation); if (origInd != newInd) { ed.Replace( Document.Editor.GetLine(ed.Caret.Line).Offset, originalIndentation, newInd); } return(false); } } return(base.KeyPress(key, keyChar, modifier)); }