Skip to content

Commit 272cb32

Browse files
authored
lsp: handle line-endings appropriately (#3318)
1 parent c3e4baf commit 272cb32

File tree

6 files changed

+61
-18
lines changed

6 files changed

+61
-18
lines changed

CMakePresets.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@
108108
"description": "Build with Clang with Debug Symbols",
109109
"inherits": ["base-windows-debug", "base-clang"]
110110
},
111+
{
112+
"name": "Debug-windows-clang-static",
113+
"displayName": "Windows Static Debug (clang)",
114+
"description": "Build with Clang as Debug but statically linked",
115+
"inherits": ["base-windows-debug", "base-clang"],
116+
"cacheVariables": {
117+
"STATICALLY_LINK": "true",
118+
"ZYDIS_BUILD_SHARED_LIB": "OFF"
119+
}
120+
},
111121
{
112122
"name": "RelWithDebInfo-windows-clang",
113123
"displayName": "Windows RelWithDebInfo (clang)",

common/util/FileUtil.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,4 +706,24 @@ std::string make_screenshot_filepath(const GameVersion game_version, const std::
706706
return file_path;
707707
}
708708

709+
std::string get_majority_file_line_endings(const std::string& file_contents) {
710+
size_t lf_count = 0;
711+
size_t crlf_count = 0;
712+
713+
for (size_t i = 0; i < file_contents.size(); ++i) {
714+
if (file_contents[i] == '\n') {
715+
if (i > 0 && file_contents[i - 1] == '\r') {
716+
crlf_count++;
717+
} else {
718+
lf_count++;
719+
}
720+
}
721+
}
722+
723+
if (crlf_count > lf_count) {
724+
return "\r\n";
725+
}
726+
return "\n";
727+
}
728+
709729
} // namespace file_util

common/util/FileUtil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ std::vector<fs::path> sort_filepaths(const std::vector<fs::path>& paths, const b
6969
/// Will overwrite the destination if it exists
7070
void copy_file(const fs::path& src, const fs::path& dst);
7171
std::string make_screenshot_filepath(const GameVersion game_version, const std::string& name = "");
72+
std::string get_majority_file_line_endings(const std::string& file_contents);
7273
} // namespace file_util

common/util/string_util.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ std::vector<std::string> split(const ::std::string& str, char delimiter) {
102102
return google_diff::split_string(str, delimiter);
103103
}
104104

105+
std::vector<std::string> split_string(const std::string& str, const std::string& delimiter) {
106+
std::vector<std::string> parsed;
107+
std::string::size_type pos = 0;
108+
while (true) {
109+
const std::string::size_type found = str.find(delimiter, pos);
110+
if (found == std::string::npos) {
111+
parsed.push_back(str.substr(pos));
112+
break;
113+
} else {
114+
parsed.push_back(str.substr(pos, found - pos));
115+
pos = found + delimiter.length();
116+
}
117+
}
118+
return parsed;
119+
}
120+
105121
std::vector<std::string> regex_get_capture_groups(const std::string& str,
106122
const std::string& regex) {
107123
std::vector<std::string> groups;

common/util/string_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ int line_count(const std::string& str);
1818
bool valid_regex(const std::string& regex);
1919
std::string diff(const std::string& lhs, const std::string& rhs);
2020
/// Default splits on \n characters
21+
/// DEPRECATED - stop using it, limited as it only takes a char
2122
std::vector<std::string> split(const ::std::string& str, char delimiter = '\n');
23+
std::vector<std::string> split_string(const std::string& str, const std::string& delimiter = "\n");
2224
std::string join(const std::vector<std::string>& strs, const std::string& join_with);
2325
std::vector<std::string> regex_get_capture_groups(const std::string& str, const std::string& regex);
2426
bool replace(std::string& str, const std::string& from, const std::string& to);

lsp/state/workspace.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ void Workspace::stop_tracking_file(const LSPSpec::DocumentUri& file_uri) {
295295

296296
WorkspaceOGFile::WorkspaceOGFile(const std::string& content, const GameVersion& game_version)
297297
: m_content(content), m_game_version(game_version) {
298-
m_lines = str_util::split(content);
298+
const auto line_ending = file_util::get_majority_file_line_endings(content);
299+
m_lines = str_util::split_string(content, line_ending);
299300
lg::info("Added new OG file. {} lines with {} symbols and {} diagnostics", m_lines.size(),
300301
m_symbols.size(), m_diagnostics.size());
301302
}
@@ -322,24 +323,17 @@ std::optional<std::string> WorkspaceOGFile::get_symbol_at_position(
322323
}
323324

324325
WorkspaceIRFile::WorkspaceIRFile(const std::string& content) {
325-
// Get all lines of file
326-
std::string::size_type pos = 0;
327-
std::string::size_type prev = 0;
328-
329-
// TODO - i hate this assignment inside a conditional, get rid of it
330-
while ((pos = content.find("\r\n", prev)) != std::string::npos) {
331-
std::string line = content.substr(prev, pos - prev);
332-
m_lines.push_back(line);
333-
// Run any checks on that line
334-
find_all_types_path(line);
335-
find_function_symbol(m_lines.size() - 1, line);
336-
identify_diagnostics(m_lines.size() - 1, line);
337-
prev = pos + 1;
326+
const auto line_ending = file_util::get_majority_file_line_endings(content);
327+
m_lines = str_util::split_string(content, line_ending);
328+
329+
for (int i = 0; i < m_lines.size(); i++) {
330+
const auto& line = m_lines.at(i);
331+
if (m_all_types_uri == "") {
332+
find_all_types_path(line);
333+
}
334+
find_function_symbol(i, line);
335+
identify_diagnostics(i, line);
338336
}
339-
std::string line = content.substr(prev);
340-
m_lines.push_back(line);
341-
find_function_symbol(m_lines.size() - 1, line);
342-
identify_diagnostics(m_lines.size() - 1, line);
343337

344338
lg::info("Added new IR file. {} lines with {} symbols and {} diagnostics", m_lines.size(),
345339
m_symbols.size(), m_diagnostics.size());

0 commit comments

Comments
 (0)