Skip to content

Commit e672fc6

Browse files
Added Auto ident.
1 parent c1d1429 commit e672fc6

File tree

13 files changed

+112
-5
lines changed

13 files changed

+112
-5
lines changed

LuaCodeView.iOS.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<copyright>Copyright © Vinicius Jarina 2019</copyright>
1717
<tags>LuaCodeView</tags>
1818
<dependencies>
19-
<dependency id="Codefoco.CYRTextView" version="0.4.0" />
19+
<dependency id="Codefoco.CYRTextView" version="0.4.2" />
2020
</dependencies>
2121
</metadata>
2222
<files>

LuaCodeView.iOS.png

-3.33 KB
Loading

LuaCodeViewExample/LuaCodeViewExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
<Reference Include="System.Core" />
8888
<Reference Include="Xamarin.iOS" />
8989
<Reference Include="CYRTextView">
90-
<HintPath>..\packages\Codefoco.CYRTextView.0.4.0\lib\xamarinios\CYRTextView.dll</HintPath>
90+
<HintPath>..\packages\Codefoco.CYRTextView.0.4.2\lib\xamarinios\CYRTextView.dll</HintPath>
9191
</Reference>
9292
</ItemGroup>
9393
<ItemGroup>

LuaCodeViewExample/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Codefoco.CYRTextView" version="0.4.0" targetFramework="xamarinios10" />
3+
<package id="Codefoco.CYRTextView" version="0.4.2" targetFramework="xamarinios10" />
44
</packages>

build/XamariniOS/LuaCodeView.iOS.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<Reference Include="System.Core" />
4545
<Reference Include="Xamarin.iOS" />
4646
<Reference Include="CYRTextView">
47-
<HintPath>..\..\packages\Codefoco.CYRTextView.0.4.0\lib\xamarinios\CYRTextView.dll</HintPath>
47+
<HintPath>..\..\packages\Codefoco.CYRTextView.0.4.2\lib\xamarinios\CYRTextView.dll</HintPath>
4848
</Reference>
4949
</ItemGroup>
5050
<ItemGroup>

build/XamariniOS/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Codefoco.CYRTextView" version="0.4.0" targetFramework="xamarinios10" />
3+
<package id="Codefoco.CYRTextView" version="0.4.2" targetFramework="xamarinios10" />
44
</packages>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/LuaCodeView.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public LuaCodeView(CGRect rect) : base(rect)
2424
Initilize();
2525
}
2626

27+
public string Tab { get; set; }
28+
2729
private void Initilize()
2830
{
2931
Tokens = new[] {
@@ -42,12 +44,117 @@ private void Initilize()
4244
UIColor.FromRGB (43, 145, 175)),
4345
new CYRToken("comment", "--.*", UIColor.FromRGB (0, 128, 0)),
4446
};
47+
48+
Tab = "\t";
4549
}
4650

4751
[Export("awakeAfterUsingCoder:")]
4852
public NSObject AwakeAfterUsingCoder(NSCoder aDecoder)
4953
{
5054
return new LuaCodeView(Frame);
5155
}
56+
57+
string FindPreviousLine(UITextPosition from)
58+
{
59+
nint offset = -1;
60+
UITextPosition prefixPosition = GetPosition(from, offset);
61+
if (prefixPosition == null)
62+
return string.Empty;
63+
64+
UITextRange range; //[textView textRangeFromPosition: prefixPosition toPosition: from];
65+
66+
while (prefixPosition != null)
67+
{
68+
range = GetTextRange(prefixPosition, from);
69+
70+
string text = TextInRange(range);
71+
if (text[0] == '\n')
72+
return text.Substring(1);
73+
74+
offset--;
75+
prefixPosition = GetPosition(from, offset);
76+
}
77+
78+
return string.Empty;
79+
}
80+
81+
int FindFirstNonWhitespace(string previousLine)
82+
{
83+
for (int i = 0; i < previousLine.Length; i++)
84+
{
85+
char ch = previousLine[i];
86+
if (!char.IsWhiteSpace(ch))
87+
return i;
88+
}
89+
return -1;
90+
}
91+
92+
string CheckAutoIdentation(string previousLine)
93+
{
94+
if (previousLine.EndsWith(" do", StringComparison.Ordinal) ||
95+
previousLine.EndsWith("\tdo", StringComparison.Ordinal) ||
96+
previousLine.Equals("do", StringComparison.Ordinal) ||
97+
previousLine.EndsWith(" then", StringComparison.Ordinal) ||
98+
previousLine.EndsWith("\tthen", StringComparison.Ordinal) ||
99+
previousLine.Equals("then", StringComparison.Ordinal) ||
100+
previousLine.EndsWith(" else", StringComparison.Ordinal) ||
101+
previousLine.EndsWith("\telse", StringComparison.Ordinal) ||
102+
previousLine.Equals("else", StringComparison.Ordinal) ||
103+
(
104+
(previousLine.Contains("function ") || previousLine.Contains("function(")) && !previousLine.EndsWith(" end", StringComparison.Ordinal)
105+
))
106+
return Tab;
107+
108+
return string.Empty;
109+
}
110+
111+
string FindIdentationPrefixWithRange(UITextPosition from)
112+
{
113+
string previousLine = FindPreviousLine(from);
114+
115+
if (string.IsNullOrEmpty(previousLine))
116+
return null;
117+
118+
string autoIdent = CheckAutoIdentation(previousLine);
119+
120+
int index = FindFirstNonWhitespace(previousLine);
121+
// No identation
122+
if (index == 0)
123+
return autoIdent;
124+
// Entire line is whitespace
125+
if (index == -1)
126+
return previousLine + autoIdent;
127+
128+
return previousLine.Substring(0, index) + autoIdent;
129+
}
130+
131+
[Export("textView:shouldChangeTextInRange:replacementText:")]
132+
public bool ShouldChangeTextWithReplacementText(UITextView textView, NSRange range, string text)
133+
{
134+
if (text != "\n")
135+
return true;
136+
137+
// Get the replacement range of the UITextView
138+
UITextPosition beginning = BeginningOfDocument;
139+
UITextPosition start = GetPosition(beginning, range.Location);
140+
UITextPosition end = GetPosition(start, range.Length);
141+
UITextRange textRange = GetTextRange(start, end);
142+
143+
string sufix = FindIdentationPrefixWithRange(start);
144+
145+
if (string.IsNullOrEmpty(sufix))
146+
return true;
147+
148+
string identation = "\n" + sufix;
149+
// Insert that newline character *and* a tab
150+
// at the point at which the user inputted just the
151+
// newline character
152+
ReplaceText(textRange, identation);
153+
154+
// Update the cursor position accordingly
155+
NSRange cursor = new NSRange(range.Location + identation.Length, 0);
156+
SelectedRange = cursor;
157+
return false;
158+
}
52159
}
53160
}

0 commit comments

Comments
 (0)