Add 'gf' (goto file) functionality
This commit is contained in:
parent
99475f93d9
commit
09901d455e
12
src/file.cc
12
src/file.cc
|
@ -185,4 +185,16 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String find_file(const String& filename, const memoryview<String>& paths)
|
||||||
|
{
|
||||||
|
for (auto path : paths)
|
||||||
|
{
|
||||||
|
String candidate = path + filename;
|
||||||
|
struct stat buf;
|
||||||
|
if (stat(candidate.c_str(), &buf) == 0 and S_ISREG(buf.st_mode))
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ String parse_filename(const String& filename);
|
||||||
String read_file(const String& filename);
|
String read_file(const String& filename);
|
||||||
Buffer* create_buffer_from_file(const String& filename);
|
Buffer* create_buffer_from_file(const String& filename);
|
||||||
void write_buffer_to_file(const Buffer& buffer, const String& filename);
|
void write_buffer_to_file(const Buffer& buffer, const String& filename);
|
||||||
|
String find_file(const String& filename, const memoryview<String>& paths);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
src/main.cc
19
src/main.cc
|
@ -93,6 +93,25 @@ void do_go(Context& context)
|
||||||
editor.select(buf.iterator_at_line_begin(buf.line_count() - 1), mode);
|
editor.select(buf.iterator_at_line_begin(buf.line_count() - 1), mode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'f':
|
||||||
|
{
|
||||||
|
String filename = context.editor().selections().back().content();
|
||||||
|
static char forbidden[] = { '\'', '\\', '\0' };
|
||||||
|
for (auto c : forbidden)
|
||||||
|
if (contains(filename, c))
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::vector<String> paths = { ""_str, "/usr/include/"_str };
|
||||||
|
const String& buffer_name = context.buffer().name();
|
||||||
|
auto it = find(reversed(buffer_name), '/');
|
||||||
|
if (it != buffer_name.rend())
|
||||||
|
paths.insert(paths.begin(), String{buffer_name.begin(), it.base()});
|
||||||
|
|
||||||
|
String path = find_file(filename, paths);
|
||||||
|
if (not path.empty())
|
||||||
|
CommandManager::instance().execute("edit '" + path + "'", context);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user