Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 1326911

Browse files
Fix caret position after formatting, now staying at the same position + Fixed odd window size and position on first program launch + changed all 'Spcode' to 'SPCode'.
1 parent 108c0d9 commit 1326911

File tree

7 files changed

+67
-35
lines changed

7 files changed

+67
-35
lines changed

Properties/Resources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Settings.Designer.cs

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Settings.settings

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="SPCode.Properties" GeneratedClassName="Settings">
33
<Profiles />
44
<Settings>
5-
<Setting Name="Height" Type="System.Double" Scope="User">
6-
<Value Profile="(Default)">0</Value>
5+
<Setting Name="Height" Type="System.String" Scope="User">
6+
<Value Profile="(Default)">Auto</Value>
77
</Setting>
8-
<Setting Name="Width" Type="System.Double" Scope="User">
9-
<Value Profile="(Default)">0</Value>
8+
<Setting Name="Width" Type="System.String" Scope="User">
9+
<Value Profile="(Default)">Auto</Value>
1010
</Setting>
11-
<Setting Name="Top" Type="System.Double" Scope="User">
12-
<Value Profile="(Default)">0</Value>
11+
<Setting Name="Top" Type="System.String" Scope="User">
12+
<Value Profile="(Default)">Auto</Value>
1313
</Setting>
14-
<Setting Name="Left" Type="System.Double" Scope="User">
15-
<Value Profile="(Default)">0</Value>
14+
<Setting Name="Left" Type="System.String" Scope="User">
15+
<Value Profile="(Default)">Auto</Value>
1616
</Setting>
1717
</Settings>
1818
</SettingsFile>

Spcode.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
<ProjectGuid>{60D2BA42-B59B-4B49-928E-C0CDDE254917}</ProjectGuid>
88
<OutputType>WinExe</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>Spcode</RootNamespace>
11-
<AssemblyName>Spcode</AssemblyName>
10+
<RootNamespace>SPCode</RootNamespace>
11+
<AssemblyName>SPCode</AssemblyName>
1212
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile />
1515
<IsWebBootstrapper>false</IsWebBootstrapper>
16+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
17+
<LangVersion>latest</LangVersion>
1618
<PublishUrl>publish\</PublishUrl>
1719
<Install>true</Install>
1820
<InstallFrom>Disk</InstallFrom>
@@ -27,8 +29,6 @@
2729
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
2830
<UseApplicationTrust>false</UseApplicationTrust>
2931
<BootstrapperEnabled>true</BootstrapperEnabled>
30-
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
31-
<LangVersion>latest</LangVersion>
3232
</PropertyGroup>
3333
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
3434
<PlatformTarget>AnyCPU</PlatformTarget>

Spedit.sln.DotSettings

Lines changed: 0 additions & 2 deletions
This file was deleted.

UI/MainWindowCommands.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.IO;
3+
using System.Linq;
34
using System.Text;
45
using System.Windows.Controls;
56
using Lysis;
@@ -217,14 +218,47 @@ private void Command_TidyCode(bool All)
217218
foreach (var ee in editors)
218219
if (ee != null)
219220
{
220-
var currentCaret = ee.editor.TextArea.Caret.Offset;
221-
var currentLen = ee.editor.Text.Length;
221+
int currentCaret = ee.editor.TextArea.Caret.Offset, numOfSpacesOrTabsBefore = 0;
222+
var line = ee.editor.Document.GetLineByOffset(currentCaret);
223+
int lineNumber = line.LineNumber;
224+
// 0 - start | any other - middle | -1 - EOS
225+
int curserLinePos = currentCaret == line.Offset ? 0 : currentCaret == line.EndOffset ? -1 : currentCaret - line.Offset;
226+
227+
if (curserLinePos > 0) numOfSpacesOrTabsBefore = ee.editor.Document.GetText(line.Offset, curserLinePos).Count(c => c == ' ' || c == '\t');
228+
229+
#if DEBUG
230+
Debug.WriteLine($"Curser offset before format: {currentCaret}");
231+
232+
// Where is out curser?
233+
if (currentCaret == line.Offset)
234+
Debug.WriteLine("Curser is at the start of the line");
235+
else if (currentCaret == line.EndOffset)
236+
Debug.WriteLine("Curser is at the end of the line");
237+
else
238+
Debug.WriteLine("Curser is somewhere in the middle of the line");
239+
#endif
240+
// Formatting Start //
222241
ee.editor.Document.BeginUpdate();
223242
var source = ee.editor.Text;
224243
ee.editor.Document.Replace(0, source.Length, SPSyntaxTidy.TidyUp(source));
225244
ee.editor.Document.EndUpdate();
226-
var diff = currentLen - ee.editor.Text.Length;
227-
ee.editor.TextArea.Caret.Offset = currentCaret + diff;
245+
// Formatting End //
246+
247+
line = ee.editor.Document.GetLineByNumber(lineNumber);
248+
int newCaretPos = line.Offset;
249+
if(curserLinePos == -1)
250+
{
251+
newCaretPos += line.Length;
252+
}
253+
else if(curserLinePos != 0)
254+
{
255+
int numOfSpacesOrTabsAfter = ee.editor.Document.GetText(line.Offset, curserLinePos).Count(c => c == ' ' || c == '\t');
256+
newCaretPos += curserLinePos + (numOfSpacesOrTabsAfter - numOfSpacesOrTabsBefore);
257+
#if DEBUG
258+
Debug.WriteLine($"Curser offset after format: {newCaretPos}");
259+
#endif
260+
}
261+
ee.editor.TextArea.Caret.Offset = newCaretPos;
228262
}
229263
}
230264

app.config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup><userSettings>
99
<SPCode.Properties.Settings>
1010
<setting name="Height" serializeAs="String">
11-
<value>0</value>
11+
<value>Auto</value>
1212
</setting>
1313
<setting name="Width" serializeAs="String">
14-
<value>0</value>
14+
<value>Auto</value>
1515
</setting>
1616
<setting name="Top" serializeAs="String">
17-
<value>0</value>
17+
<value>Auto</value>
1818
</setting>
1919
<setting name="Left" serializeAs="String">
20-
<value>0</value>
20+
<value>Auto</value>
2121
</setting>
2222
</SPCode.Properties.Settings>
2323
</userSettings>

0 commit comments

Comments
 (0)