파일을 저장할 때 각 줄의 끝에 공백 문자를 자동으로 제거하도록 Visual Studio 2008을 구성 할 수 있습니까? 내장 옵션이없는 것 같으므로이를 수행 할 수있는 확장이 있습니까?
CodeMaid는 널리 사용되는 Visual Studio 확장이며 다른 유용한 정리와 함께 자동으로 수행됩니다.
저장시 파일을 정리하도록 설정했는데 이것이 기본값이라고 생각합니다.
정규식을 사용하여 찾기/바꾸기
찾기 및 바꾸기 대화 상자에서 찾기 옵션을 확장하고 사용을 확인하고 정규 표현식을 선택하십시오.
찾을 내용 : ":Zs#$
"
대체 : ""
클릭 모두 바꾸기
다른 편집기 (normal 정규식 파서) ":Zs#$
"는"\s*$
".
저장 후이를 실행하기 위해 실행되는 매크로를 작성할 수 있습니다.
매크로의 EnvironmentEvents 모듈에 다음을 추가하십시오.
Private saved As Boolean = False
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
If Not saved Then
Try
DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
"\t", _
vsFindOptions.vsFindOptionsRegularExpression, _
" ", _
vsFindTarget.vsFindTargetCurrentDocument, , , _
vsFindResultsLocation.vsFindResultsNone)
' Remove all the trailing whitespaces.
DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
":Zs+$", _
vsFindOptions.vsFindOptionsRegularExpression, _
String.Empty, _
vsFindTarget.vsFindTargetCurrentDocument, , , _
vsFindResultsLocation.vsFindResultsNone)
saved = True
document.Save()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
Else
saved = False
End If
End Sub
나는 지금 아무런 문제없이 이것을 오랫동안 사용 해왔다. 매크로를 만들지 않았지만 빠른 Google 검색으로 찾을 수있는 ace_guidelines.vsmacros의 매크로에서 수정했습니다.
저장하기 전에 자동 서식 바로 가기를 사용할 수 있습니다 CTRL+K+D.
다음 세 가지 동작으로 쉽게 수행 할 수 있습니다.
Ctrl + A (모든 텍스트를 선택하십시오)
편집-> 고급-> 수평 공백 삭제
편집-> 고급-> 형식 선택
몇 초 기다렸다가 완료하십시오.
이것의 Ctrl + Z'뭔가 잘못 된 경우에 가능합니다.
이미 주어진 모든 답변에서 요소를 취하면 다음과 같은 코드가 있습니다. (주로 C++ 코드를 작성하지만 필요에 따라 다른 파일 확장자를 쉽게 확인할 수 있습니다.)
기여한 모든 사람에게 감사합니다!
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
Dim fileName As String
Dim result As vsFindResult
Try
fileName = document.Name.ToLower()
If fileName.EndsWith(".cs") _
Or fileName.EndsWith(".cpp") _
Or fileName.EndsWith(".c") _
Or fileName.EndsWith(".h") Then
' Remove trailing whitespace
result = DTE.Find.FindReplace( _
vsFindAction.vsFindActionReplaceAll, _
"{:b}+$", _
vsFindOptions.vsFindOptionsRegularExpression, _
String.Empty, _
vsFindTarget.vsFindTargetFiles, _
document.FullName, _
"", _
vsFindResultsLocation.vsFindResultsNone)
If result = vsFindResult.vsFindResultReplaced Then
' Triggers DocumentEvents_DocumentSaved event again
document.Save()
End If
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
End Sub
정규 검색을 사용하여 공백 제거 및 주석 다시 쓰기에 설명 된대로 매크로를 사용할 수 있습니다.
불행히도 매크로가 지원되지 않는 VWD 2010 Express를 사용하고 있습니다. 복사하기 만하면됩니다. Notepad ++ 왼쪽 상단 메뉴 Edit
> Blank Operations
> Trim Trailing Space
사용 가능한 다른 관련 작업도 있습니다. 그런 다음 Visual Studio로 다시 복사/붙여 넣기하십시오.
"소스"메뉴 아래에 "후행 공백 제거"가있는 메모장 ++ 대신 NetBeans 를 사용할 수도 있습니다.
이 프로젝트가 한 사람이 아닌 이상하지 마십시오. 로컬 파일을 소스 코드 리포지토리와 구분하는 것은 쉽지 않으므로 공백을 지우면 변경하지 않아도되는 행이 변경됩니다. 나는 완전히 이해한다. 공백을 모두 균일하게 유지하고 싶습니다. 그러나 이는보다 깔끔한 협업을 위해 포기해야하는 것입니다.
Jeff Muir 버전은 소스 코드 파일 (내 경우 C #이지만 트림을 더 쉽게 추가 할 수 있음)을 자르면 약간 향상 될 수 있다고 생각합니다. 또한 확인이없는 일부 상황에서 이상한 오류 (예 : LINQ to SQL 파일 '* .dbml')가 표시되어 문서 창이 표시되도록 확인을 추가했습니다.
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) Handles DocumentEvents.DocumentSaved
Dim result As vsFindResult
Try
If (document.ActiveWindow Is Nothing) Then
Return
End If
If (document.Name.ToLower().EndsWith(".cs")) Then
document.Activate()
result = DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, ":Zs+$", vsFindOptions.vsFindOptionsRegularExpression, String.Empty, vsFindTarget.vsFindTargetCurrentDocument, , , vsFindResultsLocation.vsFindResultsNone)
If result = vsFindResult.vsFindResultReplaced Then
document.Save()
End If
End If
Catch ex As Exception
MsgBox(ex.Message & Chr(13) & "Document: " & document.FullName, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
End Sub
나는 개인적으로 Visual Studio 2012를 통해 다시 지원하는 Trailing Whitespace Visualizer Visual Studio 확장 기능을 좋아합니다.
ArtisticStyle (C++)을 사용하여 코드를 다시 포맷합니다. 그러나이 도구를 외부 도구로 추가해야했고 마음에 들지 않도록 직접 트리거해야합니다.
그러나 코드를 수동으로 실행하는 가격을 지불 할 수있는보다 사용자 정의 방식 (예 : 멀티 라인 함수 매개 변수)으로 코드를 다시 포맷 할 수 있다는 것이 좋습니다. 도구는 무료입니다.
Dyaus의 답변과 connect report 의 정규 표현식을 바탕으로 모두 저장을 처리하고 탭을 공백으로 바꾸지 않으며 정적 변수가 필요없는 매크로가 있습니다. 가능한 단점은? FindReplace
에 대한 여러 번의 호출로 인해 약간 느려 보입니다.
Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
Try
' Remove all the trailing whitespaces.
If vsFindResult.vsFindResultReplaced = DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
"{:b}+$", _
vsFindOptions.vsFindOptionsRegularExpression, _
String.Empty, _
vsFindTarget.vsFindTargetFiles, _
document.FullName, , _
vsFindResultsLocation.vsFindResultsNone) Then
document.Save()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
End Try
End Sub
Visual Studio 2012 추가 기능 에서이 기능을 사용하려는 다른 사람은 [ \t]+(?=\r?$)
을 사용하여 정규 표현식을 사용합니다 (필요한 경우 백 슬래시를 이스케이프하는 것을 잊지 마십시오). 캐리지 리턴과 일치하지 않는 원시 변환 of {:b}+$
의 문제를 해결하기위한 몇 가지 헛된 시도 끝에 여기에 도착했습니다.
리팩터링에서 VS2010과 충돌하지 않는이 매크로 버전이 있고 텍스트가 아닌 파일을 저장할 때 IDE 멈춤)하지 않습니다.
Private Sub DocumentEvents_DocumentSaved( _
ByVal document As EnvDTE.Document) _
Handles DocumentEvents.DocumentSaved
' See if we're saving a text file
Dim textDocument As EnvDTE.TextDocument = _
TryCast(document.Object(), EnvDTE.TextDocument)
If textDocument IsNot Nothing Then
' Perform search/replace on the text document directly
' Convert tabs to spaces
Dim convertedTabs = textDocument.ReplacePattern("\t", " ", _
vsFindOptions.vsFindOptionsRegularExpression)
' Remove trailing whitespace from each line
Dim removedTrailingWS = textDocument.ReplacePattern(":Zs+$", "", _
vsFindOptions.vsFindOptionsRegularExpression)
' Re-save the document if either replace was successful
' (NOTE: Should recurse only once; the searches will fail next time)
If convertedTabs Or removedTrailingWS Then
document.Save()
End If
End If
End Sub