| 75 | } |
| 76 | |
| 77 | void FormatContext::EnableAutoDetectConfig() { |
| 78 | switch (_workMode) { |
| 79 | case WorkMode::File: { |
| 80 | if (_workspace.empty()) { |
| 81 | _workspace = std::filesystem::current_path().string(); |
| 82 | } |
| 83 | std::filesystem::path workspace(_workspace); |
| 84 | std::filesystem::path inputPath(_inputPath); |
| 85 | if (!IsSubRelative(inputPath, workspace)) { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | auto directory = absolute(inputPath.parent_path()); |
| 90 | while (IsSubRelative(directory, workspace)) { |
| 91 | auto editorconfigPath = directory / ".editorconfig"; |
| 92 | if (std::filesystem::exists(editorconfigPath)) { |
| 93 | SetConfigFilePath(editorconfigPath.string()); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | directory = directory.parent_path(); |
| 98 | } |
| 99 | } |
| 100 | case WorkMode::Stdin: { |
| 101 | if (_workspace.empty() || _inputPath.empty()) { |
| 102 | return; |
| 103 | } |
| 104 | std::filesystem::path workspace(_workspace); |
| 105 | std::filesystem::path inputPath(_inputPath); |
| 106 | if (!IsSubRelative(inputPath, workspace)) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | auto directory = absolute(inputPath.parent_path()); |
| 111 | while (IsSubRelative(directory, workspace)) { |
| 112 | auto editorconfigPath = directory / ".editorconfig"; |
| 113 | if (std::filesystem::exists(editorconfigPath)) { |
| 114 | SetConfigFilePath(editorconfigPath.string()); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | directory = directory.parent_path(); |
| 119 | } |
| 120 | } |
| 121 | default: { |
| 122 | FileFinder finder(_workspace); |
| 123 | finder.AddFindFile(".editorconfig"); |
| 124 | finder.AddIgnoreDirectory(".git"); |
| 125 | finder.AddIgnoreDirectory(".github"); |
| 126 | finder.AddIgnoreDirectory(".svn"); |
| 127 | finder.AddIgnoreDirectory(".idea"); |
| 128 | finder.AddIgnoreDirectory(".vs"); |
| 129 | finder.AddIgnoreDirectory(".vscode"); |
| 130 | |
| 131 | auto files = finder.FindFiles(); |
| 132 | |
| 133 | for (auto &file: files) { |
| 134 | std::filesystem::path filePath(file); |
no test coverage detected