質問するログイン新規登録

回答編集履歴

3

リンクあった

2024/08/31 17:16

投稿

TN8001
TN8001

スコア10132

answer CHANGED
@@ -1,4 +1,4 @@
1
- ~~[How can I remove all comments from a file of code](https://github.com/dotnet/roslyn/wiki/FAQ#how-can-i-remove-all-comments-from-a-file-of-code)~~(リンク切れ)
1
+ [How can I remove all comments from a file of code](https://github.com/dotnet/roslyn/blob/main/docs/wiki/FAQ.md#how-can-i-remove-all-comments-from-a-file-of-code)
2
2
  [FAQ(32)](https://github.com/dotnet/roslyn-sdk/blob/ad006d17388f67d40f02749e5b4fbc570be41aad/samples/CSharp/APISamples/FAQ.cs#L2025)
3
3
 
4
4
  雑でよければ上記リンクの通りで削除はできました(ただし空行が残りますorz

2

見直しキャンペーン中

2023/08/10 10:19

投稿

TN8001
TN8001

スコア10132

answer CHANGED
@@ -1,5 +1,4 @@
1
- [How can I remove all comments from a file of code](https://github.com/dotnet/roslyn/wiki/FAQ#how-can-i-remove-all-comments-from-a-file-of-code)
1
+ ~~[How can I remove all comments from a file of code](https://github.com/dotnet/roslyn/wiki/FAQ#how-can-i-remove-all-comments-from-a-file-of-code)~~(リンク切れ)
2
-
3
2
  [FAQ(32)](https://github.com/dotnet/roslyn-sdk/blob/ad006d17388f67d40f02749e5b4fbc570be41aad/samples/CSharp/APISamples/FAQ.cs#L2025)
4
3
 
5
4
  雑でよければ上記リンクの通りで削除はできました(ただし空行が残りますorz

1

見直しキャンペーン中

2023/07/20 15:01

投稿

TN8001
TN8001

スコア10132

answer CHANGED
@@ -1,77 +1,76 @@
1
- [How can I remove all comments from a file of code](https://github.com/dotnet/roslyn/wiki/FAQ#how-can-i-remove-all-comments-from-a-file-of-code)
2
-
3
- [FAQ(32)](https://github.com/dotnet/roslyn-sdk/blob/ad006d17388f67d40f02749e5b4fbc570be41aad/samples/CSharp/APISamples/FAQ.cs#L2025)
4
-
5
- 雑でよければ上記リンクの通りで削除はできました
6
- ただし空行が残りますorz
7
- ```C#
8
- using System;
9
- using Microsoft.CodeAnalysis;
10
- using Microsoft.CodeAnalysis.CSharp;
11
-
12
- namespace Questions243387
13
- {
14
- internal class Program
15
- {
16
- private static void Main()
17
- {
18
- var tree = SyntaxFactory.ParseSyntaxTree(
19
- @"using System;
20
- // 行頭にあ行コメント
21
- /* 行頭から始まる1行コメント */
22
- /*
23
- * 行頭から始まる複数行コメント
24
- */
25
- namespace ConsoleApp1
26
- {
27
- class Program // 行の途中にある一行コメント
28
- {
29
- static void Main(string[] /* 行の途中にある1行コメント */ args)
30
- {
31
- Console.WriteLine(""ダブルクォーテーションの中に含まれる「//」"");
32
- }
33
- }
34
- }");
35
-
36
- var rewriter = new CommentRemover();
37
- var newRoot = rewriter.Visit(tree.GetRoot());
38
- Console.WriteLine(newRoot.ToFullString());
39
- }
40
-
41
- private class CommentRemover : CSharpSyntaxRewriter
42
- {
43
- public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
44
- {
45
- if(trivia.Kind() == SyntaxKind.SingleLineCommentTrivia
46
- || trivia.Kind() == SyntaxKind.MultiLineCommentTrivia)
47
- {
48
- return default;
49
- }
50
- return base.VisitTrivia(trivia);
51
- }
52
- }
53
- }
54
- }
55
- ```
56
- 出力
57
- ```
58
- using System;
59
-
60
-
61
-
62
- namespace ConsoleApp1
63
- {
64
- class Program
65
- {
66
- static void Main(string[] args)
67
- {
68
- Console.WriteLine("ダブルクォーテーションの中に含まれる「//」");
69
- }
70
- }
71
- }
72
- ```
73
-
74
- ---
75
-
76
- こちらのコードでは行ごと削除できてるっぽいのですが、何をしてるのかわかりません^^;
1
+ [How can I remove all comments from a file of code](https://github.com/dotnet/roslyn/wiki/FAQ#how-can-i-remove-all-comments-from-a-file-of-code)
2
+
3
+ [FAQ(32)](https://github.com/dotnet/roslyn-sdk/blob/ad006d17388f67d40f02749e5b4fbc570be41aad/samples/CSharp/APISamples/FAQ.cs#L2025)
4
+
5
+ 雑でよければ上記リンクの通りで削除はできました(ただし空行が残りますorz
6
+ ```cs
7
+ using System;
8
+ using Microsoft.CodeAnalysis;
9
+ using Microsoft.CodeAnalysis.CSharp;
10
+
11
+ namespace Questions243387
12
+ {
13
+ internal class Program
14
+ {
15
+ private static void Main()
16
+ {
17
+ var tree = SyntaxFactory.ParseSyntaxTree(
18
+ @"using System;
19
+ // 行頭にある一行コメント
20
+ /* 行頭から始ま1行コメント */
21
+ /*
22
+ * 行頭から始まる複数行コメント
23
+ */
24
+ namespace ConsoleApp1
25
+ {
26
+ class Program // 行の途中にある一行コメント
27
+ {
28
+ static void Main(string[] /* 行の途中にある1行コメント */ args)
29
+ {
30
+ Console.WriteLine(""ダブルクォーテーションの中に含まれる「//」"");
31
+ }
32
+ }
33
+ }");
34
+
35
+ var rewriter = new CommentRemover();
36
+ var newRoot = rewriter.Visit(tree.GetRoot());
37
+ Console.WriteLine(newRoot.ToFullString());
38
+ }
39
+
40
+ private class CommentRemover : CSharpSyntaxRewriter
41
+ {
42
+ public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
43
+ {
44
+ if(trivia.Kind() == SyntaxKind.SingleLineCommentTrivia
45
+ || trivia.Kind() == SyntaxKind.MultiLineCommentTrivia)
46
+ {
47
+ return default;
48
+ }
49
+ return base.VisitTrivia(trivia);
50
+ }
51
+ }
52
+ }
53
+ }
54
+ ```
55
+ 出力
56
+ ```cs
57
+ using System;
58
+
59
+
60
+
61
+ namespace ConsoleApp1
62
+ {
63
+ class Program
64
+ {
65
+ static void Main(string[] args)
66
+ {
67
+ Console.WriteLine("ダブルクォーテーションの中に含まれる「//」");
68
+ }
69
+ }
70
+ }
71
+ ```
72
+
73
+ ---
74
+
75
+ こちらのコードでは行ごと削除できてるっぽいのですが、何をしてるのかわかりません^^;
77
76
  [CommentStripper/CommentStripperCSharpSyntaxRewriter.cs at master · MichaelKetting/CommentStripper](https://github.com/MichaelKetting/CommentStripper/blob/master/CommentStripper/CommentStripperCSharpSyntaxRewriter.cs)