mape: Add syntax highlighting for Map.c files

issue1247
Armin Burgmeier 2014-08-04 09:33:24 -04:00
parent afa32381ad
commit cd388ba178
5 changed files with 254 additions and 67 deletions

View File

@ -47,44 +47,127 @@ struct _ThreadResult {
static void mape_edit_view_set_filename(MapeEditView* view,
const gchar* filename)
{
MapeMapgenType new_type;
GtkSourceBuffer* buf;
gchar* basename;
gchar* utf8_file;
gchar* utf8_file_casefold;
gchar* utf8_landscape_txt_casefold;
const gchar* lang_name;
const gchar* lang_filename;
GtkSourceLanguage* lang;
GtkSourceStyleScheme* style;
GtkWidget* error_dialog;
buf = GTK_SOURCE_BUFFER(
gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view) )
);
/* TODO: Verify that filename is absolute and make it absolute if
it is not */
g_free(view->file_path);
view->file_path = g_strdup(filename);
if(filename != NULL)
view->file_path = g_strdup(filename);
else
view->file_path = NULL;
basename = g_path_get_basename(filename);
/* Determine document type */
utf8_file = g_filename_to_utf8(
basename,
-1,
NULL,
NULL,
NULL
);
g_free(basename);
if(utf8_file != NULL)
if(filename != NULL)
{
utf8_file_casefold = g_utf8_casefold(utf8_file, -1);
utf8_landscape_txt_casefold = g_utf8_casefold("landscape.txt", -1);
g_free(utf8_file);
basename = g_path_get_basename(filename);
if(g_utf8_collate(utf8_file_casefold, utf8_landscape_txt_casefold) == 0)
view->type = MAPE_MAPGEN_LANDSCAPE_TXT;
else
view->type = MAPE_MAPGEN_MAP_C;
g_free(utf8_file_casefold);
g_free(utf8_landscape_txt_casefold);
/* Determine document type */
utf8_file = g_filename_to_utf8(
basename,
-1,
NULL,
NULL,
NULL
);
g_free(basename);
if(utf8_file != NULL)
{
utf8_file_casefold = g_utf8_casefold(utf8_file, -1);
utf8_landscape_txt_casefold = g_utf8_casefold("landscape.txt", -1);
g_free(utf8_file);
if(g_utf8_collate(utf8_file_casefold, utf8_landscape_txt_casefold) == 0)
new_type = MAPE_MAPGEN_LANDSCAPE_TXT;
else
new_type = MAPE_MAPGEN_MAP_C;
g_free(utf8_file_casefold);
g_free(utf8_landscape_txt_casefold);
}
}
else
{
new_type = MAPE_MAPGEN_MAP_C;
}
/* TODO: Set language according to document type */
if(new_type != view->type)
{
view->type = new_type;
switch(view->type)
{
case MAPE_MAPGEN_LANDSCAPE_TXT:
lang_name = "c4landscape";
lang_filename = "Landscape.txt";
break;
case MAPE_MAPGEN_MAP_C:
lang_name = "c4mapscript";
lang_filename = "Map.c";
break;
default:
lang_name = NULL;
g_assert_not_reached();
break;
}
/* Set language according to document type */
lang = gtk_source_language_manager_get_language(
view->lang_manager,
lang_name
);
style = gtk_source_style_scheme_manager_get_scheme(
view->style_manager,
"mape"
);
if(lang == NULL || style == NULL)
{
/* TODO: Show location where we search in */
error_dialog = gtk_message_dialog_new(
NULL,
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"Syntax Highlighting Not Available"
);
gtk_message_dialog_format_secondary_text(
GTK_MESSAGE_DIALOG(error_dialog),
"The syntax highlighting file for %s files "
"could not be located. Perhaps mape has not "
"been properly installed. Syntax "
"highlighting is disabled.",
lang_filename
);
gtk_window_set_title(GTK_WINDOW(error_dialog), "Mape");
gtk_dialog_run(GTK_DIALOG(error_dialog) );
gtk_widget_destroy(error_dialog);
}
else
{
gtk_source_buffer_set_language(buf, lang);
gtk_source_buffer_set_style_scheme(buf, style);
}
}
}
static void
@ -268,9 +351,6 @@ MapeEditView* mape_edit_view_new(MapePreView* pre_view,
{
MapeEditView* view;
GtkSourceBuffer* buf;
GtkSourceLanguage* lang;
GtkSourceStyleScheme* style;
GtkWidget* error_dialog;
GPtrArray* search_dirs;
const gchar* const* data_dirs;
const gchar* const* dir;
@ -280,7 +360,7 @@ MapeEditView* mape_edit_view_new(MapePreView* pre_view,
view->statusbar = statusbar;
view->file_path = NULL;
view->encoding = "UTF-8";
view->type = MAPE_MAPGEN_MAP_C;
view->type = MAPE_MAPGEN_NONE;
view->render_thread = NULL;
view->rerender = FALSE;
view->fixed_seed = FALSE;
@ -341,41 +421,7 @@ MapeEditView* mape_edit_view_new(MapePreView* pre_view,
g_ptr_array_foreach(search_dirs, (GFunc)g_free, NULL);
g_ptr_array_free(search_dirs, TRUE);
/* TODO: Set language according to document type */
lang = gtk_source_language_manager_get_language(
view->lang_manager,
"c4landscape"
);
style = gtk_source_style_scheme_manager_get_scheme(
view->style_manager,
"c4landscape"
);
if(lang == NULL || style == NULL)
{
/* TODO: Show location where we search in */
/* TODO: Show whether we are missing the file for Landscape.txt or Map.c files */
error_dialog = gtk_message_dialog_new(
NULL,
GTK_DIALOG_MODAL,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"The syntax highlighting file for Landscape.txt files "
"could not be located. Perhaps mape has not been "
"properly installed. Syntax highlighting is disabled."
);
gtk_window_set_title(GTK_WINDOW(error_dialog), "Mape");
gtk_dialog_run(GTK_DIALOG(error_dialog) );
gtk_widget_destroy(error_dialog);
}
else
{
gtk_source_buffer_set_language(buf, lang);
gtk_source_buffer_set_style_scheme(buf, style);
}
mape_edit_view_set_filename(view, NULL);
gtk_widget_show(view->view);

View File

@ -0,0 +1,133 @@
<?xml version="1.0" encoding="UTF-8"?>
<language id="c4mapscript" _name="C4MapScript" version="2.0" _section="Sources">
<metadata>
<property name="mimetypes">text/plain</property>
<property name="globs">Map.c</property>
<property name="line-comment-start">//</property>
<property name="block-comment-start">/*</property>
<property name="block-comment-end">*/</property>
</metadata>
<styles>
<style id="comment" _name="Comment" />
<style id="literal" _name="Literal" />
<style id="keyword" _name="Keyword" />
<style id="property" _name="Property" />
</styles>
<definitions>
<context id="c4mapscript">
<include>
<context id="comment" style-ref="comment" end-at-line-end="true">
<start>//</start>
<end>^</end>
</context>
<context id="comment-multiline" style-ref="comment">
<start>/\*</start>
<end>\*/</end>
</context>
<context id="number" style-ref="literal">
<match extended="true">
(?&lt;![\w\.]) ([-+]?[0-9]+\s*) (?![\w\.])
</match>
</context>
<define-regex id="escaped-character" extended="true">
\\( # leading backslash
[\\\"] # escaped character
)
</define-regex>
<context id="string" style-ref="literal" end-at-line-end="true">
<start>"</start>
<end>"</end>
<include>
<context id="escaped-character">
<match>\%{escaped-character}</match>
</context>
</include>
</context>
<context id="boolean" style-ref="literal">
<keyword>true</keyword>
<keyword>false</keyword>
</context>
<context id="algorithms" style-ref="literal">
<keyword>MAPALGO_Layer</keyword>
<keyword>MAPALGO_RndChecker</keyword>
<keyword>MAPALGO_Rect</keyword>
<keyword>MAPALGO_Ellipsis</keyword>
<keyword>MAPALGO_Polygon</keyword>
<keyword>MAPALGO_Lines</keyword>
<keyword>MAPALGO_And</keyword>
<keyword>MAPALGO_Or</keyword>
<keyword>MAPALGO_Not</keyword>
<keyword>MAPALGO_Xor</keyword>
<keyword>MAPALGO_Offset</keyword>
<keyword>MAPALGO_Scale</keyword>
<keyword>MAPALGO_Rotate</keyword>
<keyword>MAPALGO_Turbulence</keyword>
<keyword>MAPALGO_Border</keyword>
<keyword>MAPALGO_Filter</keyword>
</context>
<context id="include-directive" style-ref="keyword">
<match>^\s*#include</match>
</context>
<context id="keywords" style-ref="keyword">
<keyword>array</keyword>
<keyword>break</keyword>
<keyword>bool</keyword>
<keyword>continue</keyword>
<keyword>do</keyword>
<keyword>else</keyword>
<keyword>for</keyword>
<keyword>func</keyword>
<keyword>id</keyword>
<keyword>if</keyword>
<keyword>int</keyword>
<keyword>object</keyword>
<keyword>private</keyword>
<keyword>proplist</keyword>
<keyword>protected</keyword>
<keyword>public</keyword>
<keyword>return</keyword>
<keyword>string</keyword>
<keyword>var</keyword>
<keyword>while</keyword>
</context>
<context id="properties" style-ref="property">
<keyword>Algo</keyword>
<keyword>Amplitude</keyword>
<keyword>Bottom</keyword>
<keyword>Distance</keyword>
<keyword>Empty</keyword>
<keyword>Filter</keyword>
<keyword>FixedOffset</keyword>
<keyword>Hgt</keyword>
<keyword>Iterations</keyword>
<keyword>Layer</keyword>
<keyword>Left</keyword>
<keyword>OffX</keyword>
<keyword>OffY</keyword>
<keyword>Op</keyword>
<keyword>Open</keyword>
<keyword>R</keyword>
<keyword>Ratio</keyword>
<keyword>Right</keyword>
<keyword>Scale</keyword>
<keyword>Seed</keyword>
<keyword>Top</keyword>
<keyword>Wdt</keyword>
<keyword>X</keyword>
<keyword>Y</keyword>
</context>
</include>
</context>
</definitions>
</language>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<style-scheme id="c4landscape" _name="C4Landscape" version="1.0">
<style-scheme id="mape" _name="Mape" version="1.0">
<author>Armin Burgmeier</author>
<_description>C4Landscape color scheme</_description>
<_description>Mape color scheme</_description>
<!-- Bracket Matching -->
<style name="bracket-match" foreground="#white" background="#grey" bold="true"/>
@ -14,4 +14,9 @@
<style name="c4landscape:attribute" foreground="#0000ff"/>
<style name="c4landscape:algorithm" foreground="#000080"/>
<style name="c4mapscript:comment" foreground="#008000"/>
<style name="c4mapscript:literal" foreground="#c00080"/>
<style name="c4mapscript:keyword" foreground="#0000ff"/>
<style name="c4mapscript:property" foreground="#c00000"/>
</style-scheme>

View File

@ -102,7 +102,8 @@ static void mape_mapgen_read_color(guint8* dest,
* @filename: The filename of the file that is being parsed. This is only used
* for display purposes.
* @source: The map generator source code for the map to generate.
* @type: Specifies how the text in @source should be interpreted.
* @type: Specifies how the text in @source should be interpreted. Must not be
* #MAPE_MAPGEN_NONE.
* @script_path: Path to the script source for algo=script overlays, or %NULL.
* @material_map: The material map containing the materials to be used during
* map generation.

View File

@ -41,12 +41,14 @@ typedef enum _MapeMapgenError {
/**
* MapeMapgenType:
* @MAPE_MAPGEN_NONE: Does not represent a map description.
* @MAPE_MAPGEN_LANDSCAPE_TXT: Represents a Landscape.txt script.
* @MAPE_MAPGEN_MAP_C: Represents a Map.c script.
*
* Specifies the different types of maps that can be rendered.
*/
typedef enum _MapeMapgenType {
MAPE_MAPGEN_NONE,
MAPE_MAPGEN_LANDSCAPE_TXT,
MAPE_MAPGEN_MAP_C
} MapeMapgenType;