-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatisticianToolWindowControl.xaml.cs
More file actions
179 lines (167 loc) · 6.41 KB
/
StatisticianToolWindowControl.xaml.cs
File metadata and controls
179 lines (167 loc) · 6.41 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace CodeLineStatistician
{
public partial class StatisticianToolWindowControl : UserControl
{
DTE2 dte;
List<string> extensionsList;
public bool IsCodeFileExtension(string extension)
{
return extensionsList.Contains(extension.ToLowerInvariant());
}
public StatisticianToolWindowControl()
{
this.InitializeComponent();
dte = Package.GetGlobalService(typeof(DTE)) as DTE2;
}
private void WalkProjectItems(ProjectItems projectItems, List<Project> projects)
{
foreach (ProjectItem projectItem in projectItems)
{
if (projectItem.SubProject != null)
{
if (string.IsNullOrEmpty(projectItem.Name)) continue;
projects.Add(projectItem.SubProject);
if (projectItem.SubProject.Kind == EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder)
{
WalkProjectItems(projectItem.SubProject.ProjectItems, projects);
}
}
}
}
public void RefreshProjectList()
{
StatisticianButton.IsEnabled = false;
ResultTextBlock.Text = "";
// vs获取解决方案中所有项目
Solution solution = dte.Solution;
List<Project> projects = new List<Project>();
foreach (Project project in solution.Projects)
{
if (string.IsNullOrEmpty(project.Name)) continue;
// 添加到项目列表
projects.Add(project);
// 如果项目是解决方案文件夹,递归添加子项目
if (project.Kind == EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder)
{
WalkProjectItems(project.ProjectItems, projects);
}
}
// 更新项目列表控件
ProjectComboBox.ItemsSource = projects;
ProjectComboBox.DisplayMemberPath = "Name";
if(projects.Count > 0) {
ProjectComboBox.SelectedIndex = 0;
StatisticianButton.IsEnabled = true;
}
}
private List<string> TraverseProjectItems(ProjectItems projectItems)
{
var files = new List<string>();
foreach (ProjectItem projectItem in projectItems)
{
// 如果项目项是文件
if (projectItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
{
string filePath = projectItem.get_FileNames(1);
if (IsCodeFileExtension(System.IO.Path.GetExtension(filePath)))
{
files.Add(filePath);
}
}
// 如果项目项是文件夹,则递归遍历其子项
else if (projectItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder)
{
files.AddRange(TraverseProjectItems(projectItem.ProjectItems));
}
}
return files;
}
private async void StatisticianButton_Click(object sender, RoutedEventArgs e)
{
if(ProjectComboBox.SelectedItem is not null)
{
var project = (Project)ProjectComboBox.SelectedItem;
var counts = 0;
ResultTextBlock.Text = "正在统计中...";
RefreshExtension();
var files = TraverseProjectItems(project.ProjectItems);
Debug.WriteLine(files.Count);
ResultTextBlock.Text = $"一共有 {files.Count} 个文件{Environment.NewLine}";
foreach (var file in files)
{
var filecount = GetFileCodeLineCount(file);
ResultTextBlock.Text += $"{file}:{filecount.ToString()}{Environment.NewLine}";
counts +=filecount;
}
MessageBox.Show(counts.ToString(),"结果",MessageBoxButton.OK,MessageBoxImage.Information);
ResultTextBlock.Text += $"总计:{counts.ToString()}";
}
else
{
MessageBox.Show("请选择项目!");
}
}
private void RefreshProjectButton_Click(object sender, RoutedEventArgs e)
{
RefreshProjectList();
}
private void RestoreButton_Click(object sender, RoutedEventArgs e)
{
extensionsInput.Text = ".cs;.cpp;.java;.js;.ts;.vb;.py;.rb;.go;.swift;.php;.html;.css;.xml";
RefreshExtension();
}
private void RefreshExtension()
{
extensionsList = extensionsInput.Text.Split(';').ToList();
}
private int GetFileCodeLineCount(string path)
{
var counts = 0;
var excludeEmptyLine = false;
if(ExcludeEmptyLineCheckBox.IsChecked is not null)
{
excludeEmptyLine = ExcludeEmptyLineCheckBox.IsChecked.Value;
}
var excludeCommentLine = false;
if (ExcludeCommentLineCheckBox.IsChecked is not null)
{
excludeCommentLine = ExcludeCommentLineCheckBox.IsChecked.Value;
}
if (File.Exists(path))
{
var lines = File.ReadLines(path).ToList();
foreach (var line in lines)
{
if (excludeEmptyLine)
{
if (string.IsNullOrWhiteSpace(line) || string.IsNullOrEmpty(line))
{
continue;
}
}
if (excludeCommentLine)
{
if (line.Trim().StartsWith("//") || line.Trim().StartsWith("#"))
{
continue;
}
}
counts++;
}
}
Debug.WriteLine(path + " " + counts);
return counts;
}
}
}