Rename Filter to Highlighter to be more explicit

This commit is contained in:
Maxime Coste 2011-11-29 22:37:20 +00:00
parent e89516f2a3
commit 0859b20bcf
14 changed files with 185 additions and 185 deletions

View File

@ -107,14 +107,14 @@ from the selection.
While inserting, _^B_ key followed by a digit inserts the designated capture. While inserting, _^B_ key followed by a digit inserts the designated capture.
Filters Highlighters
------- ------------
Manipulation of the displayed text is done through filters, which can be added Manipulation of the displayed text is done through highlighters, which can be added
or removed with the command :addfilter <filter_name> <filter_parameters...> or removed with the command :addhl <highlighter_name> <highlighter_parameters...>
and :rmfilter <filter_id> and :rmhl <highlighter_id>
existing filters are: existing highlighters are:
* *highlight_selections*: used to make current selection visible * *highlight_selections*: used to make current selection visible
* *expand_tabs*: expand tabs to next 8 multiple column (to make configurable) * *expand_tabs*: expand tabs to next 8 multiple column (to make configurable)

View File

@ -1,16 +0,0 @@
#ifndef filter_hh_INCLUDED
#define filter_hh_INCLUDED
#include <functional>
namespace Kakoune
{
class DisplayBuffer;
typedef std::function<void (DisplayBuffer& display_buffer)> FilterFunc;
typedef std::pair<std::string, FilterFunc> FilterAndId;
}
#endif // filter_hh_INCLUDED

View File

@ -1,45 +0,0 @@
#include "filter_registry.hh"
#include "exception.hh"
#include "window.hh"
namespace Kakoune
{
struct factory_not_found : public runtime_error
{
factory_not_found() : runtime_error("factory not found") {}
};
void FilterRegistry::register_factory(const std::string& name,
const FilterFactory& factory)
{
assert(m_factories.find(name) == m_factories.end());
m_factories[name] = factory;
}
void FilterRegistry::add_filter_to_window(Window& window,
const std::string& name,
const FilterParameters& parameters)
{
auto it = m_factories.find(name);
if (it == m_factories.end())
throw factory_not_found();
window.add_filter(it->second(window, parameters));
}
CandidateList FilterRegistry::complete_filter(const std::string& prefix,
size_t cursor_pos)
{
std::string real_prefix = prefix.substr(0, cursor_pos);
CandidateList result;
for (auto& filter : m_factories)
{
if (filter.first.substr(0, real_prefix.length()) == real_prefix)
result.push_back(filter.first);
}
return result;
}
}

View File

@ -1,40 +0,0 @@
#ifndef filter_registry_h_INCLUDED
#define filter_registry_h_INCLUDED
#include <string>
#include <unordered_map>
#include "filter.hh"
#include "utils.hh"
#include "completion.hh"
namespace Kakoune
{
class Window;
typedef std::vector<std::string> FilterParameters;
typedef std::function<FilterAndId (Window& window,
const FilterParameters& params)> FilterFactory;
class FilterRegistry : public Singleton<FilterRegistry>
{
public:
void register_factory(const std::string& name,
const FilterFactory& factory);
void add_filter_to_window(Window& window,
const std::string& factory_name,
const FilterParameters& parameters);
CandidateList complete_filter(const std::string& prefix,
size_t cursor_pos);
private:
std::unordered_map<std::string, FilterFactory> m_factories;
};
}
#endif // filter_registry_h_INCLUDED

View File

@ -1,11 +0,0 @@
#ifndef filters_hh_INCLUDED
#define filters_hh_INCLUDED
namespace Kakoune
{
void register_filters();
}
#endif // filters_hh_INCLUDED

16
src/highlighter.hh Normal file
View File

@ -0,0 +1,16 @@
#ifndef highlighter_hh_INCLUDED
#define highlighter_hh_INCLUDED
#include <functional>
namespace Kakoune
{
class DisplayBuffer;
typedef std::function<void (DisplayBuffer& display_buffer)> HighlighterFunc;
typedef std::pair<std::string, HighlighterFunc> HighlighterAndId;
}
#endif // highlighter_hh_INCLUDED

View File

@ -0,0 +1,45 @@
#include "highlighter_registry.hh"
#include "exception.hh"
#include "window.hh"
namespace Kakoune
{
struct factory_not_found : public runtime_error
{
factory_not_found() : runtime_error("factory not found") {}
};
void HighlighterRegistry::register_factory(const std::string& name,
const HighlighterFactory& factory)
{
assert(m_factories.find(name) == m_factories.end());
m_factories[name] = factory;
}
void HighlighterRegistry::add_highlighter_to_window(Window& window,
const std::string& name,
const HighlighterParameters& parameters)
{
auto it = m_factories.find(name);
if (it == m_factories.end())
throw factory_not_found();
window.add_highlighter(it->second(window, parameters));
}
CandidateList HighlighterRegistry::complete_highlighter(const std::string& prefix,
size_t cursor_pos)
{
std::string real_prefix = prefix.substr(0, cursor_pos);
CandidateList result;
for (auto& highlighter : m_factories)
{
if (highlighter.first.substr(0, real_prefix.length()) == real_prefix)
result.push_back(highlighter.first);
}
return result;
}
}

View File

@ -0,0 +1,40 @@
#ifndef highlighter_registry_h_INCLUDED
#define highlighter_registry_h_INCLUDED
#include <string>
#include <unordered_map>
#include "highlighter.hh"
#include "utils.hh"
#include "completion.hh"
namespace Kakoune
{
class Window;
typedef std::vector<std::string> HighlighterParameters;
typedef std::function<HighlighterAndId (Window& window,
const HighlighterParameters& params)> HighlighterFactory;
class HighlighterRegistry : public Singleton<HighlighterRegistry>
{
public:
void register_factory(const std::string& name,
const HighlighterFactory& factory);
void add_highlighter_to_window(Window& window,
const std::string& factory_name,
const HighlighterParameters& parameters);
CandidateList complete_highlighter(const std::string& prefix,
size_t cursor_pos);
private:
std::unordered_map<std::string, HighlighterFactory> m_factories;
};
}
#endif // highlighter_registry_h_INCLUDED

View File

@ -1,9 +1,9 @@
#include "filters.hh" #include "highlighters.hh"
#include "assert.hh" #include "assert.hh"
#include "window.hh" #include "window.hh"
#include "display_buffer.hh" #include "display_buffer.hh"
#include "filter_registry.hh" #include "highlighter_registry.hh"
#include <boost/regex.hpp> #include <boost/regex.hpp>
namespace Kakoune namespace Kakoune
@ -80,8 +80,8 @@ Color parse_color(const std::string& color)
return Color::Default; return Color::Default;
} }
FilterAndId colorize_regex_factory(Window& window, HighlighterAndId colorize_regex_factory(Window& window,
const FilterParameters params) const HighlighterParameters params)
{ {
if (params.size() != 3) if (params.size() != 3)
throw runtime_error("wrong parameter count"); throw runtime_error("wrong parameter count");
@ -93,7 +93,7 @@ FilterAndId colorize_regex_factory(Window& window,
std::string id = "colre'" + params[0] + "'"; std::string id = "colre'" + params[0] + "'";
return FilterAndId(id, std::bind(colorize_regex, std::placeholders::_1, return HighlighterAndId(id, std::bind(colorize_regex, std::placeholders::_1,
ex, fg_color, bg_color)); ex, fg_color, bg_color));
} }
@ -194,16 +194,16 @@ void show_line_numbers(DisplayBuffer& display_buffer)
} }
} }
template<void (*filter_func)(DisplayBuffer&)> template<void (*highlighter_func)(DisplayBuffer&)>
class SimpleFilterFactory class SimpleHighlighterFactory
{ {
public: public:
SimpleFilterFactory(const std::string& id) : m_id(id) {} SimpleHighlighterFactory(const std::string& id) : m_id(id) {}
FilterAndId operator()(Window& window, HighlighterAndId operator()(Window& window,
const FilterParameters& params) const const HighlighterParameters& params) const
{ {
return FilterAndId(m_id, FilterFunc(filter_func)); return HighlighterAndId(m_id, HighlighterFunc(highlighter_func));
} }
private: private:
std::string m_id; std::string m_id;
@ -280,10 +280,10 @@ public:
} }
static FilterAndId create(Window& window, static HighlighterAndId create(Window& window,
const FilterParameters& params) const HighlighterParameters& params)
{ {
return FilterAndId("highlight_selections", return HighlighterAndId("highlight_selections",
SelectionsHighlighter(window)); SelectionsHighlighter(window));
} }
@ -291,14 +291,14 @@ private:
const Window& m_window; const Window& m_window;
}; };
void register_filters() void register_highlighters()
{ {
FilterRegistry& registry = FilterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
registry.register_factory("highlight_selections", SelectionsHighlighter::create); registry.register_factory("highlight_selections", SelectionsHighlighter::create);
registry.register_factory("expand_tabs", SimpleFilterFactory<expand_tabulations>("expand_tabs")); registry.register_factory("expand_tabs", SimpleHighlighterFactory<expand_tabulations>("expand_tabs"));
registry.register_factory("number_lines", SimpleFilterFactory<show_line_numbers>("number_lines")); registry.register_factory("number_lines", SimpleHighlighterFactory<show_line_numbers>("number_lines"));
registry.register_factory("hlcpp", SimpleFilterFactory<colorize_cplusplus>("hlcpp")); registry.register_factory("hlcpp", SimpleHighlighterFactory<colorize_cplusplus>("hlcpp"));
registry.register_factory("regex", colorize_regex_factory); registry.register_factory("regex", colorize_regex_factory);
} }

11
src/highlighters.hh Normal file
View File

@ -0,0 +1,11 @@
#ifndef highlighters_hh_INCLUDED
#define highlighters_hh_INCLUDED
namespace Kakoune
{
void register_highlighters();
}
#endif // highlighters_hh_INCLUDED

View File

@ -1,2 +1,2 @@
hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addfilter hlcpp hook WinCreate .*\.(c|cc|cpp|cxx|C|h|hh|hpp|hxx|H) addhl hlcpp

View File

@ -7,8 +7,8 @@
#include "selectors.hh" #include "selectors.hh"
#include "assert.hh" #include "assert.hh"
#include "debug.hh" #include "debug.hh"
#include "filters.hh" #include "highlighters.hh"
#include "filter_registry.hh" #include "highlighter_registry.hh"
#include "hooks_manager.hh" #include "hooks_manager.hh"
#include <unordered_map> #include <unordered_map>
@ -433,17 +433,17 @@ void show_buffer(const CommandParameters& params, const Context& context)
main_context = Context(*buffer->get_or_create_window()); main_context = Context(*buffer->get_or_create_window());
} }
void add_filter(const CommandParameters& params, const Context& context) void add_highlighter(const CommandParameters& params, const Context& context)
{ {
if (params.size() < 1) if (params.size() < 1)
throw wrong_argument_count(); throw wrong_argument_count();
try try
{ {
FilterRegistry& registry = FilterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
FilterParameters filter_params(params.begin()+1, params.end()); HighlighterParameters highlighter_params(params.begin()+1, params.end());
registry.add_filter_to_window(*context.window, params[0], registry.add_highlighter_to_window(*context.window, params[0],
filter_params); highlighter_params);
} }
catch (runtime_error& err) catch (runtime_error& err)
{ {
@ -451,12 +451,12 @@ void add_filter(const CommandParameters& params, const Context& context)
} }
} }
void rm_filter(const CommandParameters& params, const Context& context) void rm_highlighter(const CommandParameters& params, const Context& context)
{ {
if (params.size() != 1) if (params.size() != 1)
throw wrong_argument_count(); throw wrong_argument_count();
context.window->remove_filter(params[0]); context.window->remove_highlighter(params[0]);
} }
void add_hook(const CommandParameters& params, const Context& context) void add_hook(const CommandParameters& params, const Context& context)
@ -679,11 +679,11 @@ int main(int argc, char* argv[])
{ {
init_ncurses(); init_ncurses();
CommandManager command_manager; CommandManager command_manager;
BufferManager buffer_manager; BufferManager buffer_manager;
RegisterManager register_manager; RegisterManager register_manager;
FilterRegistry filter_registry; HighlighterRegistry highlighter_registry;
HooksManager hooks_manager; HooksManager hooks_manager;
command_manager.register_command(std::vector<std::string>{ "e", "edit" }, edit, command_manager.register_command(std::vector<std::string>{ "e", "edit" }, edit,
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter{ complete_filename });
@ -697,21 +697,21 @@ int main(int argc, char* argv[])
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2) std::bind(&BufferManager::complete_buffername, &buffer_manager, _1, _2)
}); });
command_manager.register_command(std::vector<std::string>{ "af", "addfilter" }, add_filter, command_manager.register_command(std::vector<std::string>{ "ah", "addhl" }, add_highlighter,
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
std::bind(&FilterRegistry::complete_filter, &filter_registry, _1, _2) std::bind(&HighlighterRegistry::complete_highlighter, &highlighter_registry, _1, _2)
}); });
command_manager.register_command(std::vector<std::string>{ "rf", "rmfilter" }, rm_filter, command_manager.register_command(std::vector<std::string>{ "rh", "rmhl" }, rm_highlighter,
PerArgumentCommandCompleter { PerArgumentCommandCompleter {
[&](const std::string& prefix, size_t cursor_pos) [&](const std::string& prefix, size_t cursor_pos)
{ return main_context.window->complete_filterid(prefix, cursor_pos); } { return main_context.window->complete_highlighterid(prefix, cursor_pos); }
}); });
command_manager.register_command(std::vector<std::string>{ "hook" }, add_hook); command_manager.register_command(std::vector<std::string>{ "hook" }, add_hook);
command_manager.register_command(std::vector<std::string>{ "source" }, exec_commands_in_file, command_manager.register_command(std::vector<std::string>{ "source" }, exec_commands_in_file,
PerArgumentCommandCompleter{ complete_filename }); PerArgumentCommandCompleter{ complete_filename });
register_filters(); register_highlighters();
try try
{ {

View File

@ -1,7 +1,7 @@
#include "window.hh" #include "window.hh"
#include "assert.hh" #include "assert.hh"
#include "filter_registry.hh" #include "highlighter_registry.hh"
#include "hooks_manager.hh" #include "hooks_manager.hh"
#include <algorithm> #include <algorithm>
@ -54,13 +54,13 @@ Window::Window(Buffer& buffer)
{ {
m_selections.push_back(Selection(buffer.begin(), buffer.begin())); m_selections.push_back(Selection(buffer.begin(), buffer.begin()));
FilterRegistry& registry = FilterRegistry::instance(); HighlighterRegistry& registry = HighlighterRegistry::instance();
HooksManager::instance().run_hook("WinCreate", buffer.name(), HooksManager::instance().run_hook("WinCreate", buffer.name(),
Context(*this)); Context(*this));
registry.add_filter_to_window(*this, "expand_tabs", FilterParameters()); registry.add_highlighter_to_window(*this, "expand_tabs", HighlighterParameters());
registry.add_filter_to_window(*this, "highlight_selections", FilterParameters()); registry.add_highlighter_to_window(*this, "highlight_selections", HighlighterParameters());
} }
void Window::check_invariant() const void Window::check_invariant() const
@ -293,9 +293,9 @@ void Window::update_display_buffer()
m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end)); m_display_buffer.append(DisplayAtom(DisplayCoord(0,0), begin, end));
for (auto& filter : m_filters) for (auto& highlighter : m_highlighters)
{ {
filter.second(m_display_buffer); highlighter.second(m_display_buffer);
m_display_buffer.check_invariant(); m_display_buffer.check_invariant();
} }
} }
@ -343,37 +343,37 @@ std::string Window::status_line() const
return oss.str(); return oss.str();
} }
void Window::add_filter(FilterAndId&& filter) void Window::add_highlighter(HighlighterAndId&& highlighter)
{ {
for (auto it = m_filters.begin(); it != m_filters.end(); ++it) for (auto it = m_highlighters.begin(); it != m_highlighters.end(); ++it)
{ {
if (it->first == filter.first) if (it->first == highlighter.first)
throw filter_id_not_unique(filter.first); throw highlighter_id_not_unique(highlighter.first);
} }
m_filters.push_back(filter); m_highlighters.push_back(highlighter);
} }
void Window::remove_filter(const std::string& id) void Window::remove_highlighter(const std::string& id)
{ {
for (auto it = m_filters.begin(); it != m_filters.end(); ++it) for (auto it = m_highlighters.begin(); it != m_highlighters.end(); ++it)
{ {
if (it->first == id) if (it->first == id)
{ {
m_filters.erase(it); m_highlighters.erase(it);
return; return;
} }
} }
} }
CandidateList Window::complete_filterid(const std::string& prefix, CandidateList Window::complete_highlighterid(const std::string& prefix,
size_t cursor_pos) size_t cursor_pos)
{ {
std::string real_prefix = prefix.substr(0, cursor_pos); std::string real_prefix = prefix.substr(0, cursor_pos);
CandidateList result; CandidateList result;
for (auto& filter : m_filters) for (auto& highlighter : m_highlighters)
{ {
if (filter.first.substr(0, real_prefix.length()) == real_prefix) if (highlighter.first.substr(0, real_prefix.length()) == real_prefix)
result.push_back(filter.first); result.push_back(highlighter.first);
} }
return result; return result;
} }

View File

@ -7,7 +7,7 @@
#include "dynamic_buffer_iterator.hh" #include "dynamic_buffer_iterator.hh"
#include "display_buffer.hh" #include "display_buffer.hh"
#include "completion.hh" #include "completion.hh"
#include "filter.hh" #include "highlighter.hh"
namespace Kakoune namespace Kakoune
{ {
@ -87,17 +87,17 @@ public:
std::string status_line() const; std::string status_line() const;
struct filter_id_not_unique : public runtime_error struct highlighter_id_not_unique : public runtime_error
{ {
filter_id_not_unique(const std::string& id) highlighter_id_not_unique(const std::string& id)
: runtime_error("filter id not unique: " + id) {} : runtime_error("highlighter id not unique: " + id) {}
}; };
void add_filter(FilterAndId&& filter); void add_highlighter(HighlighterAndId&& highlighter);
void remove_filter(const std::string& id); void remove_highlighter(const std::string& id);
CandidateList complete_filterid(const std::string& prefix, CandidateList complete_highlighterid(const std::string& prefix,
size_t cursor_pos = std::string::npos); size_t cursor_pos = std::string::npos);
private: private:
friend class Buffer; friend class Buffer;
@ -121,8 +121,8 @@ private:
SelectionList m_selections; SelectionList m_selections;
DisplayBuffer m_display_buffer; DisplayBuffer m_display_buffer;
typedef std::vector<FilterAndId> FilterList; typedef std::vector<HighlighterAndId> HighlighterList;
FilterList m_filters; HighlighterList m_highlighters;
}; };
class IncrementalInserter class IncrementalInserter