This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4587 from mono/master-issue4566
Fixes issue #4566 Enabling code folding by default
- Loading branch information
Showing
10 changed files
with
547 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
.../MonoDevelop.SourceEditor2/Mono.TextEditor/Gui/FoldMarkerMargin.FoldMarkerMarginDrawer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// FoldMarkerMargin.cs | ||
// | ||
// Author: | ||
// Mike Krüger <mkrueger@novell.com> | ||
// | ||
// Copyright (c) 2007 Novell, Inc (http://www.novell.com) | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
// | ||
|
||
using System.Linq; | ||
using System.Collections.Generic; | ||
using Cairo; | ||
using MonoDevelop.Components; | ||
using MonoDevelop.Ide.Editor.Highlighting; | ||
|
||
namespace Mono.TextEditor | ||
{ | ||
partial class FoldMarkerMargin | ||
{ | ||
abstract class FoldMarkerMarginDrawer | ||
{ | ||
readonly FoldMarkerMargin margin; | ||
|
||
public FoldMarkerMargin Margin => margin; | ||
public MonoTextEditor Editor => margin.editor; | ||
public abstract bool AutoHide { get; } | ||
public double FoldMarkerOcapitiy { get; set; } | ||
|
||
protected FoldMarkerMarginDrawer (FoldMarkerMargin margin) | ||
{ | ||
this.margin = margin; | ||
} | ||
|
||
public abstract void OptionsChanged (); | ||
|
||
public abstract void Draw (Cairo.Context cr, Cairo.Rectangle area, DocumentLine line, int lineNumber, double x, double y, double lineHeight); | ||
|
||
protected MarginMarker GetMarginMarker (DocumentLine line) | ||
{ | ||
if (line != null) { | ||
foreach (var m in Editor.Document.GetMarkers (line)) { | ||
var mm = m as MarginMarker; | ||
if (mm != null && mm.CanDraw (Margin)) { | ||
return mm; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
protected bool IsMouseHover (IEnumerable<FoldSegment> foldings) | ||
{ | ||
return foldings.Any (s => Margin.lineHover == s.GetStartLine (Editor.Document)); | ||
} | ||
} | ||
|
||
abstract class LineStateFoldMarkerMarginDrawer : FoldMarkerMarginDrawer | ||
{ | ||
Color lineStateChangedGC, lineStateDirtyGC, backgroundColor; | ||
|
||
protected LineStateFoldMarkerMarginDrawer (FoldMarkerMargin margin) : base (margin) | ||
{ | ||
} | ||
|
||
public override void OptionsChanged () | ||
{ | ||
lineStateChangedGC = SyntaxHighlightingService.GetColor (Editor.EditorTheme, EditorThemeColors.QuickDiffChanged); | ||
lineStateDirtyGC = SyntaxHighlightingService.GetColor (Editor.EditorTheme, EditorThemeColors.QuickDiffDirty); | ||
backgroundColor = SyntaxHighlightingService.GetColor (Editor.EditorTheme, EditorThemeColors.Background); | ||
} | ||
|
||
public override void Draw (Context cr, Rectangle area, DocumentLine line, int lineNumber, double x, double y, double lineHeight) | ||
{ | ||
MarginMarker marker = GetMarginMarker (line); | ||
bool hasDrawn = false; | ||
if (marker != null) { | ||
hasDrawn = marker.DrawBackground (Editor, cr, new MarginDrawMetrics (Margin, area, line, lineNumber, x, y, lineHeight)); | ||
} | ||
|
||
if (!hasDrawn) { | ||
if (Editor.GetTextEditorData ().HighlightCaretLine && Editor.Caret.Line == lineNumber) { | ||
Editor.TextViewMargin.DrawCaretLineMarker (cr, x, y, Margin.Width, lineHeight); | ||
} else { | ||
cr.Rectangle (x, y, Margin.Width, lineHeight); | ||
cr.SetSourceColor (backgroundColor); | ||
cr.Fill (); | ||
} | ||
} | ||
|
||
var state = Editor.Document.GetLineState (line); | ||
|
||
if (Editor.Options.EnableQuickDiff && state != TextDocument.LineState.Unchanged) { | ||
var prevState = line?.PreviousLine != null ? Editor.Document.GetLineState (line.PreviousLine) : TextDocument.LineState.Unchanged; | ||
var nextState = line?.NextLine != null ? Editor.Document.GetLineState (line.NextLine) : TextDocument.LineState.Unchanged; | ||
|
||
if (state == TextDocument.LineState.Changed) { | ||
cr.SetSourceColor (lineStateChangedGC); | ||
} else if (state == TextDocument.LineState.Dirty) { | ||
cr.SetSourceColor (lineStateDirtyGC); | ||
} | ||
|
||
if ((prevState == TextDocument.LineState.Unchanged && prevState != state || | ||
nextState == TextDocument.LineState.Unchanged && nextState != state)) { | ||
FoldingScreenbackgroundRenderer.DrawRoundRectangle ( | ||
cr, | ||
prevState == TextDocument.LineState.Unchanged, | ||
nextState == TextDocument.LineState.Unchanged, | ||
x + 1, | ||
y, | ||
lineHeight / 4, | ||
Margin.Width / 4, | ||
lineHeight | ||
); | ||
} else { | ||
cr.Rectangle (x + 1, y, Margin.Width / 4, lineHeight); | ||
} | ||
cr.Fill (); | ||
} | ||
} | ||
} | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
...evelop.SourceEditor2/Mono.TextEditor/Gui/FoldMarkerMargin.VSCodeFoldMarkerMarginDrawer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// FoldMarkerMargin.cs | ||
// | ||
// Author: | ||
// Mike Krüger <mkrueger@novell.com> | ||
// | ||
// Copyright (c) 2007 Novell, Inc (http://www.novell.com) | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
// | ||
// | ||
|
||
using System.Linq; | ||
using System.Collections.Generic; | ||
using Cairo; | ||
using MonoDevelop.Components; | ||
using MonoDevelop.Ide.Editor.Highlighting; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using Mono.TextEditor.Theatrics; | ||
|
||
namespace Mono.TextEditor | ||
{ | ||
partial class FoldMarkerMargin | ||
{ | ||
sealed class VSCodeFoldMarkerMarginDrawer : LineStateFoldMarkerMarginDrawer | ||
{ | ||
double foldSegmentSize = 8; | ||
Color background, foreground, foldLine; | ||
|
||
List<FoldSegment> startFoldings = new List<FoldSegment> (); | ||
|
||
public override bool AutoHide { get => true; } | ||
|
||
internal VSCodeFoldMarkerMarginDrawer (FoldMarkerMargin margin) : base (margin) | ||
{ | ||
} | ||
|
||
public override void OptionsChanged () | ||
{ | ||
base.OptionsChanged (); | ||
background = SyntaxHighlightingService.GetColor (Editor.EditorTheme, EditorThemeColors.Background); | ||
foreground = SyntaxHighlightingService.GetColor (Editor.EditorTheme, EditorThemeColors.Foreground); | ||
foldLine = SyntaxHighlightingService.GetColor (Editor.EditorTheme, EditorThemeColors.FoldLine); | ||
} | ||
|
||
public override void Draw (Context cr, Rectangle area, DocumentLine line, int lineNumber, double x, double y, double lineHeight) | ||
{ | ||
base.Draw (cr, area, line, lineNumber, x, y, lineHeight); | ||
if (!Editor.Options.ShowFoldMargin || lineNumber > Editor.Document.LineCount || line == null) | ||
return; | ||
|
||
foldSegmentSize = Margin.Width * 4 / 6; | ||
foldSegmentSize -= (foldSegmentSize) % 2; | ||
|
||
startFoldings.Clear (); | ||
|
||
foreach (var segment in Editor.Document.GetFoldingContaining (line)) { | ||
if (segment.GetStartLine (Editor.Document)?.Offset == line.Offset) | ||
startFoldings.Add (segment); | ||
} | ||
if (startFoldings.Count == 0) | ||
return; | ||
|
||
bool isCollapsed = false; | ||
|
||
foreach (var foldSegment in startFoldings) { | ||
if (foldSegment.IsCollapsed) { | ||
isCollapsed = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!isCollapsed && FoldMarkerOcapitiy <= 0) | ||
return; | ||
DrawFoldSegment (cr, x, y, isCollapsed); | ||
} | ||
|
||
void DrawFoldSegment (Cairo.Context ctx, double x, double y, bool isCollapsed) | ||
{ | ||
var drawArea = new Cairo.Rectangle (System.Math.Floor (x + (Margin.Width - foldSegmentSize) / 2) + 0.5, | ||
System.Math.Floor (y + (Editor.LineHeight - foldSegmentSize) / 2) + 0.5, foldSegmentSize, foldSegmentSize); | ||
ctx.Rectangle (drawArea); | ||
var useSolidColor = isCollapsed || FoldMarkerOcapitiy >= 1; | ||
var drawColor = background; | ||
if (!useSolidColor) | ||
drawColor.A = FoldMarkerOcapitiy; | ||
|
||
ctx.SetSourceColor (drawColor); | ||
ctx.FillPreserve (); | ||
|
||
drawColor = foldLine; | ||
if (!useSolidColor) | ||
drawColor.A = FoldMarkerOcapitiy; | ||
|
||
ctx.SetSourceColor (drawColor); | ||
ctx.Stroke (); | ||
|
||
drawColor = foreground; | ||
if (!useSolidColor) | ||
drawColor.A = FoldMarkerOcapitiy; | ||
|
||
ctx.DrawLine (drawColor, | ||
drawArea.X + drawArea.Width * 2 / 10, | ||
drawArea.Y + drawArea.Height / 2, | ||
drawArea.X + drawArea.Width - drawArea.Width * 2 / 10, | ||
drawArea.Y + drawArea.Height / 2); | ||
|
||
if (isCollapsed) | ||
ctx.DrawLine (drawColor, | ||
drawArea.X + drawArea.Width / 2, | ||
drawArea.Y + drawArea.Height * 2 / 10, | ||
drawArea.X + drawArea.Width / 2, | ||
drawArea.Y + drawArea.Height - drawArea.Height * 2 / 10); | ||
} | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.