Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Mar 31, 2018
1 parent 89dd4b0 commit f8a816d
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 386 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ target_sources(ccls PRIVATE
src/clang_indexer.cc
src/clang_translation_unit.cc
src/clang_utils.cc
src/code_complete_cache.cc
src/command_line.cc
src/diagnostics_engine.cc
src/file_consumer.cc
Expand All @@ -189,7 +188,6 @@ target_sources(ccls PRIVATE
src/lsp.cc
src/match.cc
src/message_handler.cc
src/options.cc
src/platform_posix.cc
src/platform_win.cc
src/platform.cc
Expand Down
11 changes: 11 additions & 0 deletions src/clang_complete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,14 @@ void ClangCompleteManager::FlushAllSessions() {
preloaded_sessions_.Clear();
completion_sessions_.Clear();
}

void CodeCompleteCache::WithLock(std::function<void()> action) {
std::lock_guard<std::mutex> lock(mutex_);
action();
}

bool CodeCompleteCache::IsCacheValid(lsTextDocumentPositionParams position) {
std::lock_guard<std::mutex> lock(mutex_);
return cached_path_ == position.textDocument.uri.GetPath() &&
cached_completion_position_ == position.position;
}
15 changes: 15 additions & 0 deletions src/clang_complete.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,18 @@ struct ClangCompleteManager {
// reparsed.
ThreadedQueue<ParseRequest> parse_requests_;
};

// Cached completion information, so we can give fast completion results when
// the user erases a character. vscode will resend the completion request if
// that happens.
struct CodeCompleteCache {
// NOTE: Make sure to access these variables under |WithLock|.
std::optional<std::string> cached_path_;
std::optional<lsPosition> cached_completion_position_;
std::vector<lsCompletionItem> cached_results_;

std::mutex mutex_;

void WithLock(std::function<void()> action);
bool IsCacheValid(lsTextDocumentPositionParams position);
};
12 changes: 0 additions & 12 deletions src/code_complete_cache.cc

This file was deleted.

22 changes: 0 additions & 22 deletions src/code_complete_cache.h

This file was deleted.

27 changes: 25 additions & 2 deletions src/command_line.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// TODO: cleanup includes
#include "cache_manager.h"
#include "clang_complete.h"
#include "code_complete_cache.h"
#include "diagnostics_engine.h"
#include "file_consumer.h"
#include "import_manager.h"
Expand All @@ -13,7 +12,6 @@
#include "lsp_diagnostic.h"
#include "match.h"
#include "message_handler.h"
#include "options.h"
#include "platform.h"
#include "project.h"
#include "query.h"
Expand Down Expand Up @@ -45,6 +43,31 @@ std::string g_init_options;

namespace {

std::unordered_map<std::string, std::string> ParseOptions(int argc,
char** argv) {
std::unordered_map<std::string, std::string> output;

for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg[0] == '-') {
auto equal = arg.find('=');
if (equal != std::string::npos) {
output[arg.substr(0, equal)] = arg.substr(equal + 1);
} else if (i + 1 < argc && argv[i + 1][0] != '-')
output[arg] = argv[++i];
else
output[arg] = "";
}
}

return output;
}

bool HasOption(const std::unordered_map<std::string, std::string>& options,
const std::string& option) {
return options.find(option) != options.end();
}

// This function returns true if e2e timing should be displayed for the given
// MethodId.
bool ShouldDisplayMethodTiming(MethodType type) {
Expand Down
3 changes: 1 addition & 2 deletions src/fuzzy_match.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#pragma once

#include <string_view>

#include <limits.h>
#include <string>
#include <string_view>

class FuzzyMatcher {
public:
Expand Down
151 changes: 0 additions & 151 deletions src/lex_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,6 @@ int GetOffsetForPosition(lsPosition position, std::string_view content) {
return int(i);
}

lsPosition CharPos(std::string_view search,
char character,
int character_offset) {
lsPosition result;
size_t index = 0;
while (index < search.size()) {
char c = search[index];
if (c == character)
break;
if (c == '\n') {
result.line += 1;
result.character = 0;
} else {
result.character += 1;
}
++index;
}
assert(index < search.size());
result.character += character_offset;
return result;
}

// TODO: eliminate |line_number| param.
std::optional<lsRange> ExtractQuotedRange(int line_number, const std::string& line) {
// Find starting and ending quote.
Expand Down Expand Up @@ -237,132 +215,3 @@ TEST_SUITE("Substring") {
std::make_pair(true, 7));
}
}

TEST_SUITE("LexFunctionDeclaration") {
TEST_CASE("simple") {
std::string buffer_content = " void Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;

LexFunctionDeclaration(buffer_content, declaration, std::nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "void Foo() {\n}");
REQUIRE(newlines_after_name == 0);

LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "void Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}

TEST_CASE("ctor") {
std::string buffer_content = " Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;

LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "Foo::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}

TEST_CASE("dtor") {
std::string buffer_content = " ~Foo(); ";
lsPosition declaration = CharPos(buffer_content, '~');
std::string insert_text;
int newlines_after_name = 0;

LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "Foo::~Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}

TEST_CASE("complex return type") {
std::string buffer_content = " std::vector<int> Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;

LexFunctionDeclaration(buffer_content, declaration, std::nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "std::vector<int> Foo() {\n}");
REQUIRE(newlines_after_name == 0);

LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "std::vector<int> Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}

TEST_CASE("extra complex return type") {
std::string buffer_content = " std::function < int() > \n Foo(); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;

LexFunctionDeclaration(buffer_content, declaration, std::nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "std::function < int() > \n Foo() {\n}");
REQUIRE(newlines_after_name == 0);

LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "std::function < int() > \n Type::Foo() {\n}");
REQUIRE(newlines_after_name == 0);
}

TEST_CASE("parameters") {
std::string buffer_content = "void Foo(int a,\n\n int b); ";
lsPosition declaration = CharPos(buffer_content, 'F');
std::string insert_text;
int newlines_after_name = 0;

LexFunctionDeclaration(buffer_content, declaration, std::nullopt, &insert_text,
&newlines_after_name);
REQUIRE(insert_text == "void Foo(int a,\n\n int b) {\n}");
REQUIRE(newlines_after_name == 2);

LexFunctionDeclaration(buffer_content, declaration, std::string("Type"),
&insert_text, &newlines_after_name);
REQUIRE(insert_text == "void Type::Foo(int a,\n\n int b) {\n}");
REQUIRE(newlines_after_name == 2);
}
}

TEST_SUITE("LexWordAroundPos") {
TEST_CASE("edges") {
std::string content = "Foobar";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'F'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'o'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'b'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'a'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'r'), content) == "Foobar");
}

TEST_CASE("simple") {
std::string content = " Foobar ";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'F'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'o'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'b'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'a'), content) == "Foobar");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'r'), content) == "Foobar");
}

TEST_CASE("underscores, numbers and ::") {
std::string content = " file:ns::_my_t5ype7 ";
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'f'), content) == "file");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 's'), content) == "ns");
REQUIRE(LexIdentifierAroundPos(CharPos(content, 'y'), content) ==
"ns::_my_t5ype7");
}

TEST_CASE("dot, dash, colon are skipped") {
std::string content = "1. 2- 3:";
REQUIRE(LexIdentifierAroundPos(CharPos(content, '1'), content) == "1");
REQUIRE(LexIdentifierAroundPos(CharPos(content, '2'), content) == "2");
REQUIRE(LexIdentifierAroundPos(CharPos(content, '3'), content) == "3");
}
}
4 changes: 0 additions & 4 deletions src/lex_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

// Utility method to map |position| to an offset inside of |content|.
int GetOffsetForPosition(lsPosition position, std::string_view content);
// Utility method to find a position for the given character.
lsPosition CharPos(std::string_view search,
char character,
int character_offset = 0);

// TODO: eliminate |line_number| param.
std::optional<lsRange> ExtractQuotedRange(int line_number, const std::string& line);
Expand Down
1 change: 0 additions & 1 deletion src/messages/text_document_completion.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "clang_complete.h"
#include "code_complete_cache.h"
#include "fuzzy_match.h"
#include "include_complete.h"
#include "message_handler.h"
Expand Down
1 change: 0 additions & 1 deletion src/messages/text_document_signature_help.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "clang_complete.h"
#include "code_complete_cache.h"
#include "message_handler.h"
#include "queue_manager.h"
#include "timer.h"
Expand Down
30 changes: 0 additions & 30 deletions src/options.cc

This file was deleted.

9 changes: 0 additions & 9 deletions src/options.h

This file was deleted.

3 changes: 0 additions & 3 deletions src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ void CopyFileTo(const std::string& destination, const std::string& source);

bool IsSymLink(const std::string& path);

// Returns any clang arguments that are specific to the current platform.
std::vector<const char*> GetPlatformClangArguments();

// Free any unused memory and return it to the system.
void FreeUnusedMemory();

Expand Down
4 changes: 0 additions & 4 deletions src/platform_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ bool IsSymLink(const std::string& path) {
return lstat(path.c_str(), &buf) == 0 && S_ISLNK(buf.st_mode);
}

std::vector<const char*> GetPlatformClangArguments() {
return {};
}

void FreeUnusedMemory() {
#if defined(__GLIBC__)
malloc_trim(0);
Expand Down
Loading

0 comments on commit f8a816d

Please sign in to comment.