forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabTextManager.cs
More file actions
156 lines (148 loc) · 5.09 KB
/
TabTextManager.cs
File metadata and controls
156 lines (148 loc) · 5.09 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using PluginCore;
using PluginCore.Managers;
namespace FlashDevelop.Managers
{
class TabTextManager
{
/// <summary>
/// Updates the documents tab texts
/// </summary>
public static void UpdateTabTexts()
{
if (Globals.Settings.DisableTabDifferentiation)
{
foreach (var doc in Globals.MainForm.Documents)
{
if (doc.IsEditable)
{
String name = Path.GetFileName(doc.FileName);
if (doc.IsModified) doc.Text = name + "*";
else doc.Text = name;
}
}
}
else DifferentiateTabTexts();
}
/// <summary>
/// Sets the tab text if needed
/// </summary>
public static void SetTabText(ITabbedDocument doc, String text)
{
if (doc.Text != text) doc.Text = text;
}
/// <summary>
/// Updates the tab texts by differenting them
/// </summary>
private static void DifferentiateTabTexts()
{
var byName = new Dictionary<String, List<String>>();
foreach (var doc in Globals.MainForm.Documents)
{
if (doc.IsEditable)
{
String fileName = doc.FileName;
String name = Path.GetFileName(fileName);
if (!byName.ContainsKey(name)) byName[name] = new List<String>();
byName[name].Add(doc.FileName);
}
}
foreach (var entry in byName)
{
if (entry.Value.Count == 1)
{
var doc = DocumentManager.FindDocument(entry.Value[0]);
if (doc.IsModified) SetTabText(doc, entry.Key + "*");
else SetTabText(doc, entry.Key);
}
else
{
var paths = Discriminate(entry.Value);
foreach (var path in paths)
{
var doc = DocumentManager.FindDocument(path.Tab);
if (doc.IsModified) SetTabText(doc, entry.Key + " (" + path.Diff + ")" + "*");
else SetTabText(doc, entry.Key + " (" + path.Diff + ")");
}
}
}
}
/// <summary>
/// Collects a list of path differences
/// </summary>
private static List<ExplodePath> Discriminate(List<String> tabs)
{
var paths = new List<ExplodePath>();
ExplodePath.Longer = 0;
foreach (var tab in tabs)
{
paths.Add(new ExplodePath(tab));
}
paths.Sort(ExplodePath.LongerFirst);
bool hadDiff = false;
bool hadMatch = false;
for (var i = 0; i < ExplodePath.Longer; i++)
{
bool notMatch = false;
bool hasMatch = false;
String match = paths[0][i];
for (var j = 1; j < paths.Count; j++)
{
var path = paths[j];
String part = path[i];
if (part == null) continue;
if (part != match) notMatch = true;
else hasMatch = true;
}
if (notMatch)
{
foreach (var path in paths)
{
if (path[i] == null) continue;
if (path.Diff.Length > 0)
{
path.Diff = Path.DirectorySeparatorChar + path.Diff;
if (hadMatch) path.Diff = Path.DirectorySeparatorChar + "..." + path.Diff;
}
path.Diff = path[i] + path.Diff;
}
if (!hasMatch) break;
hadMatch = false;
if (!hasMatch) hadDiff = true;
}
else if (hadDiff) break;
else hadMatch = true;
}
return paths;
}
}
class ExplodePath
{
public String Tab;
public String Diff;
public String[] Parts;
public static Int32 Longer = 0;
public Int32 Length { get { return Parts.Length; } }
public ExplodePath(String tab)
{
Tab = tab;
String path = tab;
String[] parts = Regex.Split(Path.GetDirectoryName(path), "[\\\\/]+");
Array.Reverse(parts);
if (parts.Length > Longer) Longer = parts.Length;
Parts = parts;
Diff = "";
}
public static Int32 LongerFirst(ExplodePath a, ExplodePath b)
{
return a.Length - b.Length;
}
public String this[Int32 i]
{
get { return i < Parts.Length ? Parts[i] : null; }
}
}
}