From d044a50a3a1688991a46e9084cadb2cb6710029d Mon Sep 17 00:00:00 2001 From: Sup Yut Sum Date: Tue, 1 Jul 2014 13:29:06 +0800 Subject: [PATCH] TortoiseGitMerge: Add CEditorConfigWrapper class which calls editorconfig Based on TortoiseSVN revision 25633 Signed-off-by: Sup Yut Sum --- src/TortoiseGit.sln | 1 + src/TortoiseMerge/EditorConfigWrapper.cpp | 101 ++++++++++++++++++++++++ src/TortoiseMerge/EditorConfigWrapper.h | 80 +++++++++++++++++++ src/TortoiseMerge/TortoiseMerge.vcxproj | 7 +- src/TortoiseMerge/TortoiseMerge.vcxproj.filters | 6 ++ 5 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 src/TortoiseMerge/EditorConfigWrapper.cpp create mode 100644 src/TortoiseMerge/EditorConfigWrapper.h diff --git a/src/TortoiseGit.sln b/src/TortoiseGit.sln index e4359c0d2..3fa069e5f 100644 --- a/src/TortoiseGit.sln +++ b/src/TortoiseGit.sln @@ -78,6 +78,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SshAskPass", "SshAskPass\Ss EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TortoiseGitMerge", "TortoiseMerge\TortoiseMerge.vcxproj", "{8ABB4F84-891A-4748-8507-F5494842173E}" ProjectSection(ProjectDependencies) = postProject + {DA843306-3D6D-4198-890E-25E6177E01C3} = {DA843306-3D6D-4198-890E-25E6177E01C3} {5C6B6A95-2053-4593-9617-C4F176736D5A} = {5C6B6A95-2053-4593-9617-C4F176736D5A} EndProjectSection EndProject diff --git a/src/TortoiseMerge/EditorConfigWrapper.cpp b/src/TortoiseMerge/EditorConfigWrapper.cpp new file mode 100644 index 000000000..3eac60353 --- /dev/null +++ b/src/TortoiseMerge/EditorConfigWrapper.cpp @@ -0,0 +1,101 @@ +// TortoiseGitMerge - a Diff/Patch program + +// Copyright (C) 2014 - TortoiseSVN + +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// + +#include "stdafx.h" +#include "EditorConfigWrapper.h" +#include "UnicodeUtils.h" +#include "editorconfig/editorconfig.h" + +bool CEditorConfigWrapper::Load(CString filename) +{ + if (filename.IsEmpty()) + return false; + CStringA filenameA = CUnicodeUtils::GetUTF8(filename); + editorconfig_handle eh = editorconfig_handle_init(); + if (editorconfig_parse(filenameA, eh)) + { + editorconfig_handle_destroy(eh); + return false; + } + + int count = editorconfig_handle_get_name_value_count(eh); + for (int i = 0; i < count; ++i) + { + const char* name, *value; + editorconfig_handle_get_name_value(eh, i, &name, &value); + if (!strcmp(name, "indent_style")) + { + if (!strcmp(value, "space")) + m_bIndentStyle = true; + else if (!strcmp(value, "tab")) + m_bIndentStyle = false; + else + m_bIndentStyle = nullptr; + } + else if (!strcmp(name, "indent_size")) + m_nIndentSize = atoi(value); + else if (!strcmp(name, "tab_width")) + m_nTabWidth = atoi(value); + else if (!strcmp(name, "end_of_line")) + { + if (!strcmp(value, "lf")) + m_EndOfLine = EOL::EOL_LF; + else if (!strcmp(value, "cr")) + m_EndOfLine = EOL::EOL_CR; + else if (!strcmp(value, "crlf")) + m_EndOfLine = EOL::EOL_CRLF; + else + m_EndOfLine = nullptr; + } + else if (!strcmp(name, "charset")) + { + if (!strcmp(value, "utf-8")) + m_Charset = CFileTextLines::UnicodeType::UTF8; + else if (!strcmp(value, "utf-8-bom")) + m_Charset = CFileTextLines::UnicodeType::UTF8BOM; + else if (!strcmp(value, "utf-16be")) + m_Charset = CFileTextLines::UnicodeType::UTF16_BE; + else if (!strcmp(value, "utf-16le")) + m_Charset = CFileTextLines::UnicodeType::UTF16_LE; + else + m_Charset = nullptr; + } + else if (!strcmp(name, "trim_trailing_whitespace")) + { + if (!strcmp(value, "true")) + m_bTrimTrailingWhitespace = true; + else if (!strcmp(value, "false")) + m_bTrimTrailingWhitespace = false; + else + m_bTrimTrailingWhitespace = nullptr; + } + else if (!strcmp(name, "insert_final_newline")) + { + if (!strcmp(value, "true")) + m_bInsertFinalNewline = true; + else if (!strcmp(value, "false")) + m_bInsertFinalNewline = false; + else + m_bInsertFinalNewline = nullptr; + } + } + + editorconfig_handle_destroy(eh); + return true; +} diff --git a/src/TortoiseMerge/EditorConfigWrapper.h b/src/TortoiseMerge/EditorConfigWrapper.h new file mode 100644 index 000000000..337812675 --- /dev/null +++ b/src/TortoiseMerge/EditorConfigWrapper.h @@ -0,0 +1,80 @@ +// TortoiseGitMerge - a Diff/Patch program + +// Copyright (C) 2014 - TortoiseSVN + +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software Foundation, +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// + +#pragma once +#include "FileTextLines.h" + +class CEditorConfigWrapper +{ +public: + bool Load(CString filename); + + template class Nullable + { + T m_Value; + bool m_bNull; + + public: + Nullable() + : m_bNull(true) + { + } + + Nullable(T value) + : m_bNull(false) + , m_Value(value) + { + } + + operator T() + { + return m_Value; + } + + void operator = (T value) + { + m_bNull = false; + m_Value = value; + } + + void operator = (void *ptrnull) + { + if (ptrnull == nullptr) + m_bNull = true; + } + + bool operator == (void* ptrnull) + { + return ptrnull == nullptr ? m_bNull : false; + } + + bool operator != (void* ptrnull) + { + return ptrnull == nullptr ? !m_bNull : false; + } + }; + + Nullable m_bIndentStyle; + Nullable m_nIndentSize; + Nullable m_nTabWidth; + Nullable m_EndOfLine; + Nullable m_Charset; + Nullable m_bTrimTrailingWhitespace; + Nullable m_bInsertFinalNewline; +}; diff --git a/src/TortoiseMerge/TortoiseMerge.vcxproj b/src/TortoiseMerge/TortoiseMerge.vcxproj index 435198ab4..9ca40a5df 100644 --- a/src/TortoiseMerge/TortoiseMerge.vcxproj +++ b/src/TortoiseMerge/TortoiseMerge.vcxproj @@ -40,7 +40,7 @@ - ..\TortoiseMerge;..\..\ext\gitdll;..\git;.\svninclude;..\..\ext\apr\include;..\..\ext\apr-util\include;.\libsvn_diff;..\Utils;..\;..\Utils\MiscUI;..\..\ext\ResizableLib;..\..\ext\libgit2\include;..\..\ext\zlib;%(AdditionalIncludeDirectories) + ..\TortoiseMerge;..\..\ext\gitdll;..\git;.\svninclude;..\..\ext\apr\include;..\..\ext\apr-util\include;.\libsvn_diff;..\Utils;..\;..\Utils\MiscUI;..\..\ext\ResizableLib;..\..\ext\libgit2\include;..\..\ext\zlib;..\..\ext\editorconfig\include;%(AdditionalIncludeDirectories) APR_DECLARE_STATIC;APU_DECLARE_STATIC;%(PreprocessorDefinitions) @@ -61,6 +61,7 @@ + @@ -314,6 +315,7 @@ + @@ -402,6 +404,9 @@ + + {da843306-3d6d-4198-890e-25e6177e01c3} + {4472028d-4acf-474e-aa95-9b7e12b50f60} false diff --git a/src/TortoiseMerge/TortoiseMerge.vcxproj.filters b/src/TortoiseMerge/TortoiseMerge.vcxproj.filters index 3655e49e8..f07b3b3ba 100644 --- a/src/TortoiseMerge/TortoiseMerge.vcxproj.filters +++ b/src/TortoiseMerge/TortoiseMerge.vcxproj.filters @@ -327,6 +327,9 @@ Utils + + Source Files + @@ -521,6 +524,9 @@ Utils\Header Files + + Header Files + -- 2.11.4.GIT