forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnippetHelper.cs
More file actions
138 lines (128 loc) · 5.61 KB
/
Copy pathSnippetHelper.cs
File metadata and controls
138 lines (128 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using PluginCore.Utilities;
using ScintillaNet;
namespace PluginCore.Helpers
{
public class SnippetHelper
{
public const String BOUNDARY = "$(Boundary)";
public const String ENTRYPOINT = "$(EntryPoint)";
public const String EXITPOINT = "$(ExitPoint)";
/// <summary>
/// Processes the snippet and template arguments
/// </summary>
public static Int32 PostProcessSnippets(ScintillaControl sci, Int32 currentPosition)
{
Int32 delta = 0;
while (sci.SelectText(BOUNDARY, 0) != -1) { sci.ReplaceSel(""); delta -= BOUNDARY.Length; }
String text = sci.Text; // Store text temporarily
Int32 entryPosition = sci.MBSafePosition(text.IndexOf(ENTRYPOINT));
Int32 exitPosition = sci.MBSafePosition(text.IndexOf(EXITPOINT));
if (entryPosition != -1 && exitPosition != -1)
{
sci.SelectText(ENTRYPOINT, 0); sci.ReplaceSel(""); delta -= ENTRYPOINT.Length;
sci.SelectText(EXITPOINT, 0); sci.ReplaceSel(""); delta -= EXITPOINT.Length;
sci.SetSel(entryPosition, exitPosition - ENTRYPOINT.Length);
}
else if (entryPosition != -1 && exitPosition == -1)
{
sci.SelectText(ENTRYPOINT, 0); sci.ReplaceSel(""); delta -= ENTRYPOINT.Length;
sci.SetSel(entryPosition, entryPosition);
}
else sci.SetSel(currentPosition, currentPosition);
return delta;
}
/// <summary>
/// Processes the text and returns correct action point
/// </summary>
public static ActionPoint ProcessActionPoint(String text)
{
text = text.Trim().Replace(BOUNDARY, "");
Int32 entryPosition = text.IndexOf(ENTRYPOINT);
Int32 exitPosition = text.IndexOf(EXITPOINT);
if (entryPosition != -1 && exitPosition != -1)
{
String cleaned = text.Replace(ENTRYPOINT, "").Replace(EXITPOINT, "");
return new ActionPoint(cleaned, entryPosition, exitPosition - ENTRYPOINT.Length);
}
else if (entryPosition != -1 && exitPosition == -1)
{
String cleaned = text.Replace(ENTRYPOINT, "");
return new ActionPoint(cleaned, entryPosition, -1);
}
else return new ActionPoint(text, -1, -1);
}
/// <summary>
/// Selects the text specified in the action point
/// </summary>
public static void ExecuteActionPoint(ActionPoint point, ScintillaControl sci)
{
if (point.EntryPosition != -1 && point.ExitPosition != -1)
{
Int32 start = sci.MBSafePosition(point.EntryPosition);
Int32 end = sci.MBSafePosition(point.ExitPosition);
sci.SetSel(start, end);
}
else if (point.EntryPosition != -1 && point.ExitPosition == -1)
{
Int32 start = sci.MBSafePosition(point.EntryPosition);
sci.SetSel(start, start);
}
}
/// <summary>
/// Inserts the specified snippet to the document
/// </summary>
public static Int32 InsertSnippetText(ScintillaControl sci, Int32 currentPosition, String snippet)
{
sci.BeginUndoAction();
try
{
Int32 newIndent;
String text = snippet;
if (sci.SelTextSize > 0)
currentPosition -= sci.MBSafeTextLength(sci.SelText);
Int32 line = sci.LineFromPosition(currentPosition);
Int32 indent = sci.GetLineIndentation(line);
sci.ReplaceSel("");
Int32 lineMarker = LineEndDetector.DetectNewLineMarker(text, sci.EOLMode);
String newline = LineEndDetector.GetNewLineMarker(lineMarker);
if (newline != "\n") text = text.Replace(newline, "\n");
newline = LineEndDetector.GetNewLineMarker((Int32)PluginBase.MainForm.Settings.EOLMode);
text = PluginBase.MainForm.ProcessArgString(text).Replace(newline, "\n");
newline = LineEndDetector.GetNewLineMarker(sci.EOLMode);
String[] splitted = text.Trim().Split('\n');
for (Int32 j = 0; j < splitted.Length; j++)
{
if (j != splitted.Length - 1) sci.InsertText(sci.CurrentPos, splitted[j] + newline);
else sci.InsertText(sci.CurrentPos, splitted[j]);
sci.CurrentPos += sci.MBSafeTextLength(splitted[j]) + newline.Length;
if (j > 0)
{
line = sci.LineFromPosition(sci.CurrentPos - newline.Length);
newIndent = sci.GetLineIndentation(line) + indent;
sci.SetLineIndentation(line, newIndent);
}
}
Int32 length = sci.CurrentPos - currentPosition - newline.Length;
Int32 delta = PostProcessSnippets(sci, currentPosition);
return length + delta;
}
finally
{
sci.EndUndoAction();
}
}
}
public class ActionPoint
{
public String Text;
public Int32 EntryPosition;
public Int32 ExitPosition;
public ActionPoint(String text, Int32 entryPosition, Int32 exitPosition)
{
this.Text = text;
this.EntryPosition = entryPosition;
this.ExitPosition = exitPosition;
}
}
}