Skip to content

Commit

Permalink
Handling of CamelCase in the spell checker
Browse files Browse the repository at this point in the history
The problem is that my source code has a lot of camel case identifiers,
dictated by our coding standard. I want to use the spell checker but
it marks every camel case identifier as a miss-spelled word.

This is a simple patch to the spell checking that interprets a lower
case letter followed by an uppercase as a word boundary.

Change-Id: I6f581d0bd15f2a41787df8db6853bd4d3db92913
  • Loading branch information
benjamint-bsquare committed Nov 8, 2016
1 parent a1f4cb9 commit 04948bb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions runtime/doc/options.txt
Expand Up @@ -6863,6 +6863,16 @@ A jump table for the options with a short description can be found at |Q_op|.
When on spell checking will be done. See |spell|.
The languages are specified with 'spelllang'.

*'spellcamelcase'* *'scc'*
*'nospellcamelcase'* *'noscc'*
'spellcamelcase' boolean (default off)
local to window
{not in Vi}
{not available when compiled without the |+syntax|
feature}
When on spell checking will treat each word in a camelCase identifier
as a distinct word.

*'spellcapcheck'* *'spc'*
'spellcapcheck' 'spc' string (default "[.?!]\_[\])'" \t]\+")
local to buffer
Expand Down
9 changes: 9 additions & 0 deletions src/option.c
Expand Up @@ -231,6 +231,7 @@
#define PV_SCROLL OPT_WIN(WV_SCROLL)
#ifdef FEAT_SPELL
# define PV_SPELL OPT_WIN(WV_SPELL)
# define PV_SCC OPT_WIN(WV_SCC)
#endif
#ifdef FEAT_SYN_HL
# define PV_CUC OPT_WIN(WV_CUC)
Expand Down Expand Up @@ -2456,6 +2457,13 @@ static struct vimoption options[] =
(char_u *)VAR_WIN, PV_SPELL,
#else
(char_u *)NULL, PV_NONE,
#endif
{(char_u *)FALSE, (char_u *)0L} SCRIPTID_INIT},
{"spellcamelcase", "scc", P_BOOL|P_VI_DEF|P_RWIN,
#ifdef FEAT_SPELL
(char_u *)VAR_WIN, PV_SCC,
#else
(char_u *)NULL, PV_NONE,
#endif
{(char_u *)FALSE, (char_u *)0L} SCRIPTID_INIT},
{"spellcapcheck", "spc", P_STRING|P_ALLOCED|P_VI_DEF|P_RBUF,
Expand Down Expand Up @@ -10331,6 +10339,7 @@ get_varp(struct vimoption *p)
case PV_LIST: return (char_u *)&(curwin->w_p_list);
#ifdef FEAT_SPELL
case PV_SPELL: return (char_u *)&(curwin->w_p_spell);
case PV_SCC: return (char_u *)&(curwin->w_p_scc);
#endif
#ifdef FEAT_SYN_HL
case PV_CUC: return (char_u *)&(curwin->w_p_cuc);
Expand Down
1 change: 1 addition & 0 deletions src/option.h
Expand Up @@ -1165,6 +1165,7 @@ enum
, WV_SCROLL
#ifdef FEAT_SPELL
, WV_SPELL
, WV_SCC
#endif
#ifdef FEAT_SYN_HL
, WV_CUC
Expand Down
14 changes: 13 additions & 1 deletion src/spell.c
Expand Up @@ -435,6 +435,7 @@ spell_check(
int wrongcaplen = 0;
int lpi;
int count_word = docount;
int camel_case = 0;

/* A word never starts at a space or a control character. Return quickly
* then, skipping over the character. */
Expand Down Expand Up @@ -466,10 +467,15 @@ spell_check(
mi.mi_fend = ptr;
if (spell_iswordp(mi.mi_fend, wp))
{
int last_char_upper;
int this_char_upper = SPELL_ISUPPER(*mi.mi_fend);
do
{
last_char_upper = this_char_upper;
mb_ptr_adv(mi.mi_fend);
} while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
this_char_upper = SPELL_ISUPPER(*mi.mi_fend);
camel_case = wp->w_p_scc && !last_char_upper && this_char_upper;
} while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp) && !camel_case);

if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
{
Expand Down Expand Up @@ -500,6 +506,12 @@ spell_check(
MAXWLEN + 1);
mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);

// Introduce a fake word end space into the folded word if the word is camel case
if (camel_case)
{
mi.mi_fword[mi.mi_fwordlen-1] = ' ';
}

/* The word is bad unless we recognize it. */
mi.mi_result = SP_BAD;
mi.mi_result2 = SP_BAD;
Expand Down
2 changes: 2 additions & 0 deletions src/structs.h
Expand Up @@ -223,6 +223,8 @@ typedef struct
#ifdef FEAT_SPELL
int wo_spell;
# define w_p_spell w_onebuf_opt.wo_spell /* 'spell' */
int wo_scc;
# define w_p_scc w_onebuf_opt.wo_scc /* 'spellcamelcase' */
#endif
#ifdef FEAT_SYN_HL
int wo_cuc;
Expand Down

0 comments on commit 04948bb

Please sign in to comment.