Skip to content

Commit

Permalink
Fix minor highlighting bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDTill committed Sep 3, 2023
1 parent 608f547 commit 0e52b67
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 10 deletions.
26 changes: 26 additions & 0 deletions app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ void MainWindow::hideProjectBrowser() noexcept {
ui->actionShow_project_browser->setChecked(false);
}

void MainWindow::reparse() {
editor->getModel()->performSemanticFormatting();
editor->updateModel();
onTextChanged();
}

#ifdef FORSCAPE_WORKAROUND_QT_LINUX_FILETYPE_FILTER_BUG
class FileFilterProxyModel : public QSortFilterProxyModel {
protected:
Expand Down Expand Up @@ -751,6 +757,26 @@ void MainWindow::openProject(QString path){
onTextChanged();
}

void MainWindow::removeFile(Forscape::Typeset::Model* model) noexcept {
Forscape::Program::instance()->removeFile(model);

auto removed = std::remove_if(viewing_chain.begin(), viewing_chain.end(),
[model](const JumpPoint& v) noexcept { return v.model == model; });

while(removed != viewing_chain.end()){
viewing_chain.erase(removed);

for(size_t i = viewing_chain.size(); i-->1;)
if(viewing_chain[i] == viewing_chain[i-1])
viewing_chain[i].model = nullptr;

removed = std::remove_if(viewing_chain.begin(), viewing_chain.end(),
[](const JumpPoint& v) noexcept { return v.model == nullptr; });
}

updateViewJumpPointElements();
}

void MainWindow::addPlot(const std::string& title, const std::string& x_label, const std::string& y_label){
active_plot = new Plot(title, x_label, y_label);
active_plot->show();
Expand Down
6 changes: 6 additions & 0 deletions app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MainWindow : public QMainWindow{
MainWindow(QWidget* parent = nullptr);
virtual ~MainWindow();
void openProject(QString path);
void removeFile(Forscape::Typeset::Model* model) noexcept;
QSettings settings;
Forscape::Typeset::Editor* editor;

Expand All @@ -44,6 +45,7 @@ public slots:
void viewModel(Forscape::Typeset::Model* model);
void on_actionNew_triggered();
void hideProjectBrowser() noexcept;
void reparse();

private:
SearchDialog* search;
Expand Down Expand Up @@ -78,6 +80,10 @@ public slots:
JumpPoint() noexcept = default;
JumpPoint(Forscape::Typeset::Model* model, size_t line) noexcept :
model(model), line(line){}

bool operator==(const JumpPoint& other) const noexcept {
return model == other.model && line == other.line;
}
};
std::vector<JumpPoint> viewing_chain;
size_t viewing_index = 0;
Expand Down
23 changes: 14 additions & 9 deletions app/projectbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ProjectBrowser::FileEntry : public QTreeWidgetItem {
void setModel(Forscape::Typeset::Model& m) noexcept;
bool isSavedToDisk() const noexcept;
const std::filesystem::path& getPath() const noexcept;
void deleteFile() noexcept;
};

ProjectBrowser::FileEntry::FileEntry(QTreeWidgetItem* parent) : QTreeWidgetItem(parent) {
Expand All @@ -63,6 +64,12 @@ const std::filesystem::path& ProjectBrowser::FileEntry::getPath() const noexcept
return path;
}

void ProjectBrowser::FileEntry::deleteFile() noexcept {
if(isSavedToDisk()) std::filesystem::remove(path);
delete model;
delete this;
}

static bool itemIsFile(const QTreeWidgetItem* item) noexcept {
return item->childCount() == 0;
}
Expand Down Expand Up @@ -214,8 +221,7 @@ void ProjectBrowser::setCurrentlyViewed(Forscape::Typeset::Model* model) {
bold_font.setBold(true);

//Remove highlighting on the old item
if(currently_viewed_file)
currently_viewed_file->setFont(0, QFont());
if(currently_viewed_file) currently_viewed_file->setFont(0, QFont());

//Highlight the new item
const auto lookup = entries.find(model->path);
Expand Down Expand Up @@ -257,16 +263,15 @@ void ProjectBrowser::onShowInExplorer() {
}

void ProjectBrowser::onDeleteFile() {
FileEntry* item = debug_cast<FileEntry*>(currentItem());
if(item->isSavedToDisk()) std::filesystem::remove(item->path);

assert(item->model != Forscape::Program::instance()->program_entry_point);
delete item->model;
delete item;
FileEntry& item = getSelectedFileEntry();
assert(item.model != Forscape::Program::instance()->program_entry_point);
entries.erase(item.path);
main_window->removeFile(item.model);
item.deleteFile();

//DO THIS: this has implications for the viewing buffer

//editor->updateModel(); //DO THIS: update the model now that imports may be broken
main_window->reparse();
}

void ProjectBrowser::onRightClick(const QPoint& pos) {
Expand Down
5 changes: 5 additions & 0 deletions src/forscape_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ void Program::getFileSuggestions(std::vector<std::string>& suggestions, std::str
}
}

void Program::removeFile(Typeset::Model* model) noexcept {
source_files.erase(model->path);
all_files.erase(std::remove(all_files.begin(), all_files.end(), model), all_files.end());
}

Program::ptr_or_code Program::openFromRelativePathSpecifiedExtension(std::filesystem::path rel_path){
for(const std::filesystem::path& path_entry : project_path){
std::filesystem::path abs_path = (path_entry / rel_path).lexically_normal();
Expand Down
1 change: 1 addition & 0 deletions src/forscape_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Program {
void runStaticPass();
void getFileSuggestions(std::vector<std::string>& suggestions, Typeset::Model* active) const;
void getFileSuggestions(std::vector<std::string>& suggestions, std::string_view input, Typeset::Model* active) const;
void removeFile(Typeset::Model* model) noexcept;
std::string run();
void runThread();
void stop();
Expand Down
6 changes: 6 additions & 0 deletions src/forscape_symbol_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ void Symbol::getAllOccurences(std::vector<Typeset::Selection>& found) const {
found.push_back(usage->sel);
}

void Symbol::getModelOccurences(std::vector<Typeset::Selection>& found, Typeset::Model* model) const {
for(SymbolUsage* usage = last_external_usage; usage != nullptr; usage = usage->prevUsage())
if(usage->sel.getModel() == model)
found.push_back(usage->sel);
}

SymbolUsage::SymbolUsage() noexcept
: prev_usage_index(NONE), symbol_index(NONE) {}

Expand Down
1 change: 1 addition & 0 deletions src/forscape_symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private: friend SymbolLexicalPass; friend SymbolTable; //Fields for the lexical
void getLocalOccurences(std::vector<Typeset::Selection>& found) const;
void getExternalOccurences(std::vector<Typeset::Selection>& found) const;
void getAllOccurences(std::vector<Typeset::Selection>& found) const;
void getModelOccurences(std::vector<Typeset::Selection>& found, Typeset::Model* model) const;
};

struct ScopeSegment {
Expand Down
2 changes: 1 addition & 1 deletion src/typeset_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ void View::populateHighlightWordsFromParseNode(ParseNode pn){
else if(parse_tree.getOp(pn) != Code::OP_IDENTIFIER) return;

const Code::Symbol* const sym = parse_tree.getSymbol(pn);
if(sym) sym->getAllOccurences(highlighted_words);
if(sym) sym->getModelOccurences(highlighted_words, getModel());
}

bool View::scrolledToBottom() const noexcept {
Expand Down

0 comments on commit 0e52b67

Please sign in to comment.