summaryrefslogtreecommitdiff
path: root/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp
blob: dad71ef73d94939d7f8b98e0d5bd4408ed56ab0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "FileDB.h"

#include <AK/LexicalPath.h>
#include <AK/NonnullRefPtr.h>

namespace LanguageServers {

RefPtr<const GUI::TextDocument> FileDB::get_document(DeprecatedString const& filename) const
{
    auto absolute_path = to_absolute_path(filename);
    auto document_optional = m_open_files.get(absolute_path);
    if (!document_optional.has_value())
        return nullptr;

    return *document_optional.value();
}

RefPtr<GUI::TextDocument> FileDB::get_document(DeprecatedString const& filename)
{
    auto document = reinterpret_cast<FileDB const*>(this)->get_document(filename);
    if (document.is_null())
        return nullptr;
    return adopt_ref(*const_cast<GUI::TextDocument*>(document.leak_ref()));
}

Optional<DeprecatedString> FileDB::get_or_read_from_filesystem(StringView filename) const
{
    auto absolute_path = to_absolute_path(filename);
    auto document = get_document(absolute_path);
    if (document)
        return document->text();

    auto document_or_error = create_from_filesystem(absolute_path);
    if (document_or_error.is_error()) {
        dbgln("Failed to create document '{}': {}", absolute_path, document_or_error.error());
        return {};
    }
    return document_or_error.value()->text();
}

bool FileDB::is_open(DeprecatedString const& filename) const
{
    return m_open_files.contains(to_absolute_path(filename));
}

bool FileDB::add(DeprecatedString const& filename, int fd)
{
    auto document_or_error = create_from_fd(fd);
    if (document_or_error.is_error()) {
        dbgln("Failed to create document: {}", document_or_error.error());
        return false;
    }

    m_open_files.set(to_absolute_path(filename), document_or_error.release_value());
    return true;
}

DeprecatedString FileDB::to_absolute_path(DeprecatedString const& filename) const
{
    if (LexicalPath { filename }.is_absolute()) {
        return filename;
    }
    if (m_project_root.is_null())
        return filename;
    return LexicalPath { DeprecatedString::formatted("{}/{}", m_project_root, filename) }.string();
}

ErrorOr<NonnullRefPtr<GUI::TextDocument>> FileDB::create_from_filesystem(DeprecatedString const& filename) const
{
    auto file = TRY(Core::Stream::File::open(to_absolute_path(filename), Core::Stream::OpenMode::Read));
    return create_from_file(move(file));
}

ErrorOr<NonnullRefPtr<GUI::TextDocument>> FileDB::create_from_fd(int fd) const
{
    auto file = TRY(Core::Stream::File::adopt_fd(fd, Core::Stream::OpenMode::Read));
    return create_from_file(move(file));
}

class DefaultDocumentClient final : public GUI::TextDocument::Client {
public:
    virtual ~DefaultDocumentClient() override = default;
    virtual void document_did_append_line() override {};
    virtual void document_did_insert_line(size_t) override {};
    virtual void document_did_remove_line(size_t) override {};
    virtual void document_did_remove_all_lines() override {};
    virtual void document_did_change(GUI::AllowCallback) override {};
    virtual void document_did_set_text(GUI::AllowCallback) override {};
    virtual void document_did_set_cursor(const GUI::TextPosition&) override {};
    virtual void document_did_update_undo_stack() override { }

    virtual bool is_automatic_indentation_enabled() const override { return false; }
    virtual int soft_tab_width() const override { return 4; }
};
static DefaultDocumentClient s_default_document_client;

ErrorOr<NonnullRefPtr<GUI::TextDocument>> FileDB::create_from_file(NonnullOwnPtr<Core::Stream::File> file) const
{
    auto content = TRY(file->read_until_eof());
    auto document = GUI::TextDocument::create(&s_default_document_client);
    document->set_text(content);
    return document;
}

void FileDB::on_file_edit_insert_text(DeprecatedString const& filename, DeprecatedString const& inserted_text, size_t start_line, size_t start_column)
{
    VERIFY(is_open(filename));
    auto document = get_document(filename);
    VERIFY(document);
    GUI::TextPosition start_position { start_line, start_column };
    document->insert_at(start_position, inserted_text, &s_default_document_client);

    dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text());
}

void FileDB::on_file_edit_remove_text(DeprecatedString const& filename, size_t start_line, size_t start_column, size_t end_line, size_t end_column)
{
    // TODO: If file is not open - need to get its contents
    // Otherwise- somehow verify that respawned language server is synced with all file contents
    VERIFY(is_open(filename));
    auto document = get_document(filename);
    VERIFY(document);
    GUI::TextPosition start_position { start_line, start_column };
    GUI::TextRange range {
        GUI::TextPosition { start_line, start_column },
        GUI::TextPosition { end_line, end_column }
    };

    document->remove(range);
    dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text());
}

RefPtr<GUI::TextDocument> FileDB::create_with_content(DeprecatedString const& content)
{
    StringView content_view(content);
    auto document = GUI::TextDocument::create(&s_default_document_client);
    document->set_text(content_view);
    return document;
}

bool FileDB::add(DeprecatedString const& filename, DeprecatedString const& content)
{
    auto document = create_with_content(content);
    if (!document) {
        VERIFY_NOT_REACHED();
        return false;
    }

    m_open_files.set(to_absolute_path(filename), document.release_nonnull());
    return true;
}

}