Author Topic: Tips to create a plugin that shows underlines misspelled words in comments.  (Read 2609 times)

tbisson

  • Junior Community Member
  • Posts: 8
  • Hero Points: 0
Hi,

I would like to create a plugin that underlines misspelled words in the comments of a file. I would like to create something similar to how Sublime Edit underline in red the current misspelled words in the comments. See attachment.

I've seen the SlickEdit plugin that can highlight a specific word you select word:
http://community.slickedit.com/index.php/topic,3918.msg16388.html#msg16388

Before I get started in this endeavor, I was wondering if it possible to do this:
* Periodically pass in all words in the comments for the current/all open files to spellcheck and then determine if they should be underlined/highlighted?

Thanks,
Tim
« Last Edit: August 27, 2013, 10:13:30 pm by tbisson »

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Yes, it's possible.

To help you get started, here is some code that spell checks one word, and highlights it if misspelled.  It can also clear existing spelling highlights.  It's lifted out of one of my macro files, and it probably won't compile as-is, you'll need to do some tweaking etc.

Code: [Select]
/*
 * Spell checking.
 */

static int s_timer = -1;
static int s_markerType = -1;

definit()
{
   s_timer = -1;
   s_markerType = _MarkerTypeAlloc();
}

static void ClearSpellHighlights()
{
   if ( s_timer >= 0 )
      _kill_timer( s_timer );
   s_timer = -1;

   _StreamMarkerRemoveAllType( s_markerType );
   refresh();
}

static void SpellTimerCallback()
{
   ClearSpellHighlights();
}

_command void SpellCheckWord() name_info(','VSARG2_EDITORCTL|VSARG2_READ_ONLY)
{
   typeless p;
   save_pos( p );

   _str old_mark;
   save_selection( old_mark );
   _str mark=_duplicate_selection( '' );
   _deselect( mark );

   _str old_word_chars = p_word_chars;
   p_word_chars = "A-Za-z'";
   int start_col;
   //$ todo: (chrisant) Should check the word under the cursor, or immediately
   // left of the cursor.  Should not check "foo" in this case:  "foo \c", for
   // example (where cursor is at \c).
   prev_word();
   _str check_word = cur_word2( start_col, VSCURWORD_AT_END_USE_PREV );
   start_col = _text_colc( start_col, 'I' );
   p_word_chars = old_word_chars;

   if ( check_word != '' )
   {
      int len = _rawLength( check_word );

      p_col = start_col;
      long start_ofs = _QROffset();
      _select_char( mark );
      p_col += len;
      _select_char( mark );
      p_col -= len;  // Put cursor at beginning of word so undo works properly.
      _show_selection( mark );

      _str word;
      _str replace_word;
      int col;
      int status = _spell_check( word, replace_word, col, check_word );
      boolean fOk = ( status == SPELL_NO_MORE_WORDS_RC );

      int style = VSMARKERTYPEFLAG_DRAW_SQUIGGLY;
      int color = fOk ? _rgb( 0, 255, 0 ) : _rgb( 255, 0, 0 );
      int timeout = fOk ? 1 : 5;

      ClearSpellHighlights();
      if ( !fOk )
      {
         _MarkerTypeSetFlags( s_markerType, style|VSMARKERTYPEFLAG_AUTO_REMOVE );
         int pos_marker = _StreamMarkerAdd(
            p_window_id, start_ofs, len, true, 0, s_markerType, '' );
         _StreamMarkerSetStyleColor( pos_marker, color );
         s_timer = _set_timer( timeout * 1000, SpellTimerCallback );
      }

      end_select();  // Put cursor just after word checked.
      _deselect( mark );
   }

   restore_selection( old_mark );
   restore_pos( p );
}