Skip to content

Commit

Permalink
Implement auto-completion scope for comment styles, issue #553, #338.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed May 10, 2023
1 parent eadb01a commit 3ff3623
Show file tree
Hide file tree
Showing 76 changed files with 252 additions and 185 deletions.
83 changes: 71 additions & 12 deletions src/EditAutoC.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ static uint32_t CharacterPrefixMask[8];
static uint32_t RawStringStyleMask[8];
static uint32_t GenericTypeStyleMask[8];
static uint32_t IgnoreWordStyleMask[8];
static uint32_t CommentStyleMask[8];

// from scintilla/lexlib/DocUtils.h
#define js_style(style) ((style) + SCE_PHP_LABEL + 1)
Expand All @@ -552,10 +553,6 @@ static inline bool IsGenericTypeStyle(int style) {
return BitTestEx(GenericTypeStyleMask, style);
}

static inline bool IsWordStyleToIgnore(int style) {
return BitTestEx(IgnoreWordStyleMask, style);
}

bool IsAutoCompletionWordCharacter(uint32_t ch) {
if (ch < 0x80) {
return IsDocWordChar(ch);
Expand Down Expand Up @@ -780,7 +777,7 @@ void EscapeRegex(LPSTR pszOut, LPCSTR pszIn) {
*pszOut++ = '\0';
}

static void AutoC_AddDocWord(struct WordList *pWList, bool bIgnoreCase, char prefix) {
static void AutoC_AddDocWord(struct WordList *pWList, const uint32_t ignoredStyleMask[8], bool bIgnoreCase, char prefix) {
LPCSTR const pRoot = pWList->pWordStart;
const int iRootLen = pWList->iStartLen;

Expand Down Expand Up @@ -827,7 +824,7 @@ static void AutoC_AddDocWord(struct WordList *pWList, bool bIgnoreCase, char pre
Sci_Position wordEnd = iPosFind + iRootLen;
const int style = SciCall_GetStyleIndexAt(wordEnd - 1);
wordEnd = ft.chrgText.cpMax;
if (iPosFind != iCurrentPos && !IsWordStyleToIgnore(style)) {
if (iPosFind != iCurrentPos && !BitTestEx(ignoredStyleMask, style)) {
// find all word after '::', '->', '.' and '-'
bool bSubWord = false;
while (wordEnd < iDocLen) {
Expand Down Expand Up @@ -1447,7 +1444,7 @@ void EditCompleteUpdateConfig(void) {

static bool EditCompleteWordCore(int iCondition, bool autoInsert) {
const Sci_Position iCurrentPos = SciCall_GetCurrentPos();
const int iCurrentStyle = SciCall_GetStyleIndexAt(iCurrentPos);
int iCurrentStyle = SciCall_GetStyleIndexAt(iCurrentPos);
const Sci_Line iLine = SciCall_LineFromPosition(iCurrentPos);
const Sci_Position iLineStartPos = SciCall_PositionFromLine(iLine);

Expand Down Expand Up @@ -1604,20 +1601,38 @@ static bool EditCompleteWordCore(int iCondition, bool autoInsert) {
}
}

bool retry = false;
if (iCurrentStyle == 0) {
// word not yet styled, or is plain text
iCurrentStyle = SciCall_GetStyleIndexAt(iStartWordPos);
}

bool retry = true;
uint32_t ignoredStyleMask[8];
const bool bScanWordsInDocument = autoCompletionConfig.bScanWordsInDocument;
do {
if (!(autoCompletionConfig.fCompleteScope & AutoCompleteScope_Commont) && BitTestEx(CommentStyleMask, iCurrentStyle)) {
retry = false;
}
if (retry && bScanWordsInDocument) {
memcpy(ignoredStyleMask, IgnoreWordStyleMask, sizeof(IgnoreWordStyleMask));
if (!(autoCompletionConfig.fScanWordScope & AutoCompleteScope_Commont) && !BitTestEx(CommentStyleMask, iCurrentStyle)) {
for (UINT i = 0; i < 8; i++) {
ignoredStyleMask[i] |= CommentStyleMask[i];
}
}
}

while (retry) {
if (!bIgnoreLexer) {
// keywords
AutoC_AddKeyword(&pWList, iCurrentStyle);
}
if (bScanWordsInDocument) {
if (!bIgnoreDoc || pWList.nWordCount == 0) {
AutoC_AddDocWord(&pWList, bIgnoreCase, prefix);
AutoC_AddDocWord(&pWList, ignoredStyleMask, bIgnoreCase, prefix);
}
if (prefix && pWList.nWordCount == 0) {
prefix = '\0';
AutoC_AddDocWord(&pWList, bIgnoreCase, prefix);
AutoC_AddDocWord(&pWList, ignoredStyleMask, bIgnoreCase, prefix);
}
}

Expand All @@ -1638,7 +1653,7 @@ static bool EditCompleteWordCore(int iCondition, bool autoInsert) {
}
}
}
} while (retry);
}

#if 0
StopWatch_Stop(watch);
Expand Down Expand Up @@ -2773,6 +2788,7 @@ void InitAutoCompletionCache(LPCEDITLEXER pLex) {
memset(RawStringStyleMask, 0, sizeof(RawStringStyleMask));
memset(GenericTypeStyleMask, 0, sizeof(GenericTypeStyleMask));
memset(IgnoreWordStyleMask, 0, sizeof(IgnoreWordStyleMask));
memset(CommentStyleMask, 0, sizeof(CommentStyleMask));
memcpy(CurrentWordCharSet, DefaultWordCharSet, sizeof(DefaultWordCharSet));
//CurrentWordCharSet['.' >> 5] |= (1 << ('.' & 31));

Expand Down Expand Up @@ -2935,6 +2951,18 @@ void InitAutoCompletionCache(LPCEDITLEXER pLex) {
CurrentWordCharSet['-' >> 5] |= (1 << ('-' & 31));
CurrentWordCharSet['.' >> 5] |= (1 << ('.' & 31));
CurrentWordCharSet[':' >> 5] |= (1 << (':' & 31));
CommentStyleMask[SCE_H_COMMENT >> 5] |= (1U << (SCE_H_COMMENT & 31));
CommentStyleMask[SCE_H_XCCOMMENT >> 5] |= (1U << (SCE_H_XCCOMMENT & 31));
CommentStyleMask[SCE_H_SGML_COMMENT >> 5] |= (1U << (SCE_H_SGML_COMMENT & 31));
CommentStyleMask[SCE_H_SGML_1ST_PARAM_COMMENT >> 5] |= (1U << (SCE_H_SGML_1ST_PARAM_COMMENT & 31));
CommentStyleMask[SCE_HJ_COMMENT >> 5] |= (1U << (SCE_HJ_COMMENT & 31));
CommentStyleMask[SCE_HJ_COMMENTLINE >> 5] |= (1U << (SCE_HJ_COMMENTLINE & 31));
CommentStyleMask[SCE_HJ_COMMENTDOC >> 5] |= (1U << (SCE_HJ_COMMENTDOC & 31));
CommentStyleMask[SCE_HJA_COMMENT >> 5] |= (1U << (SCE_HJA_COMMENT & 31));
CommentStyleMask[SCE_HJA_COMMENTLINE >> 5] |= (1U << (SCE_HJA_COMMENTLINE & 31));
CommentStyleMask[SCE_HJA_COMMENTDOC >> 5] |= (1U << (SCE_HJA_COMMENTDOC & 31));
CommentStyleMask[SCE_HB_COMMENTLINE >> 5] |= (1U << (SCE_HB_COMMENTLINE & 31));
CommentStyleMask[SCE_HBA_COMMENTLINE >> 5] |= (1U << (SCE_HBA_COMMENTLINE & 31));
break;

case NP2LEX_JAVA:
Expand All @@ -2953,6 +2981,12 @@ void InitAutoCompletionCache(LPCEDITLEXER pLex) {
CurrentWordCharSet['@' >> 5] |= (1 << ('@' & 31));
break;

case NP2LEX_JSON:
CurrentWordCharSet['.' >> 5] |= (1 << ('.' & 31));
CommentStyleMask[SCE_JSON_LINECOMMENT >> 5] |= (1U << (SCE_JSON_LINECOMMENT & 31));
CommentStyleMask[SCE_JSON_BLOCKCOMMENT >> 5] |= (1U << (SCE_JSON_BLOCKCOMMENT & 31));
break;

case NP2LEX_JULIA:
CurrentWordCharSet['$' >> 5] |= (1 << ('$' & 31));
CurrentWordCharSet['.' >> 5] |= (1 << ('.' & 31));
Expand Down Expand Up @@ -2988,6 +3022,12 @@ void InitAutoCompletionCache(LPCEDITLEXER pLex) {
CurrentWordCharSet['.' >> 5] |= (1 << ('.' & 31));
break;

case NP2LEX_MARKDOWN:
CurrentWordCharSet['.' >> 5] |= (1 << ('.' & 31));
CommentStyleMask[SCE_H_COMMENT >> 5] |= (1U << (SCE_H_COMMENT & 31));
CommentStyleMask[SCE_H_SGML_COMMENT >> 5] |= (1U << (SCE_H_SGML_COMMENT & 31));
break;

case NP2LEX_PERL:
CurrentWordCharSet['$' >> 5] |= (1 << ('$' & 31));
CurrentWordCharSet['.' >> 5] |= (1 << ('.' & 31));
Expand All @@ -3002,6 +3042,20 @@ void InitAutoCompletionCache(LPCEDITLEXER pLex) {
CurrentWordCharSet[':' >> 5] |= (1 << (':' & 31));
RawStringStyleMask[SCE_PHP_STRING_SQ >> 5] |= (1U << (SCE_PHP_STRING_SQ & 31));
RawStringStyleMask[SCE_PHP_NOWDOC >> 5] |= (1U << (SCE_PHP_NOWDOC & 31));
CommentStyleMask[SCE_H_COMMENT >> 5] |= (1U << (SCE_H_COMMENT & 31));
CommentStyleMask[SCE_H_SGML_COMMENT >> 5] |= (1U << (SCE_H_SGML_COMMENT & 31));
CommentStyleMask[SCE_PHP_COMMENTLINE >> 5] |= (1U << (SCE_PHP_COMMENTLINE & 31));
CommentStyleMask[SCE_PHP_COMMENTBLOCK >> 5] |= (1U << (SCE_PHP_COMMENTBLOCK & 31));
CommentStyleMask[SCE_PHP_COMMENTBLOCKDOC >> 5] |= (1U << (SCE_PHP_COMMENTBLOCKDOC & 31));
CommentStyleMask[SCE_PHP_COMMENTTAGAT >> 5] |= (1U << (SCE_PHP_COMMENTTAGAT & 31));
CommentStyleMask[SCE_PHP_TASKMARKER >> 5] |= (1U << (SCE_PHP_TASKMARKER & 31));
CommentStyleMask[js_style(SCE_JS_COMMENTLINE) >> 5] |= (1U << (js_style(SCE_JS_COMMENTLINE) & 31));
CommentStyleMask[js_style(SCE_JS_COMMENTBLOCK) >> 5] |= (1U << (js_style(SCE_JS_COMMENTBLOCK) & 31));
CommentStyleMask[js_style(SCE_JS_COMMENTBLOCKDOC) >> 5] |= (1U << (js_style(SCE_JS_COMMENTBLOCKDOC) & 31));
CommentStyleMask[js_style(SCE_JS_COMMENTTAGAT) >> 5] |= (1U << (js_style(SCE_JS_COMMENTTAGAT) & 31));
CommentStyleMask[js_style(SCE_JS_TASKMARKER) >> 5] |= (1U << (js_style(SCE_JS_TASKMARKER) & 31));
CommentStyleMask[css_style(SCE_CSS_COMMENTBLOCK) >> 5] |= (1U << (css_style(SCE_CSS_COMMENTBLOCK) & 31));
CommentStyleMask[css_style(SCE_CSS_CDO_CDC) >> 5] |= (1U << (css_style(SCE_CSS_CDO_CDC) & 31));
break;

case NP2LEX_POWERSHELL:
Expand Down Expand Up @@ -3155,5 +3209,10 @@ void InitAutoCompletionCache(LPCEDITLEXER pLex) {
//Cache--Autogenerated -- end of section automatically generated
}

const uint32_t marker = pLex->commentStyleMarker;
if (marker) {
CommentStyleMask[0] |= (1U << (marker + 1)) - 2;
}

UpdateLexerExtraKeywords();
}
16 changes: 7 additions & 9 deletions src/EditLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ enum {
typedef struct EDITSTYLE {
const int iStyle;
// set with EDITSTYLE_HOLE() or EDITSTYLE_HOLEX()
const uint16_t rid;
const uint16_t iNameLen;
const wchar_t * const pszName;
wchar_t *szValue;
const uint16_t rid;
const uint16_t iNameLen;
const wchar_t * const pszName;
wchar_t *szValue;
const wchar_t * const pszDefault;
} EDITSTYLE, *PEDITSTYLE;

Expand All @@ -79,7 +79,6 @@ typedef struct EDITLEXER {
const int iLexer;
const int rid;
// default settings for this scheme, auto generated from LexerConfig.py
struct {
const uint32_t lexerAttr;
const uint8_t defaultTabWidth;
const uint8_t defaultIndentWidth;
Expand All @@ -96,9 +95,9 @@ typedef struct EDITLEXER {
const uint8_t operatorStyle2;

const uint64_t keywordAttr;
};
// set with EDITLEXER_HOLE() or EDITLEXER_TEXT()
struct {

const uint8_t commentStyleMarker;
// set with EDITLEXER_HOLE() or EDITLEXER_TEXT()
uint8_t iStyleTheme;
bool bStyleChanged;
bool bUseDefaultCodeStyle;
Expand All @@ -109,7 +108,6 @@ typedef struct EDITLEXER {
const wchar_t * const pszName;
wchar_t *szExtensions;
wchar_t *szStyleBuf;
};
const wchar_t * const pszDefExt;
const KEYWORDLIST * const pKeyWords;
EDITSTYLE * const Styles;
Expand Down
7 changes: 3 additions & 4 deletions src/EditLexers/EditStyleX.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

//Scheme Default Settings++Autogenerated -- start of section automatically generated
#define SCHEME_SETTINGS_DEFAULT \
{ \
LexerAttr_NoLineComment | \
LexerAttr_NoBlockComment, \
TAB_WIDTH_4, INDENT_WIDTH_4, \
Expand All @@ -17,11 +16,11 @@
0, 0, \
0, 0, \
KeywordAttr_Default \
}
, 0 \
//Scheme Default Settings--Autogenerated -- end of section automatically generated

#define EDITLEXER_HOLE(name, styles) { StyleTheme_Default, false, true, 0, COUNTOF(styles), CSTRLEN(name), (name), NULL, NULL }
#define EDITLEXER_TEXT(name, styles) { StyleTheme_Default, false, false, 0, COUNTOF(styles), CSTRLEN(name), (name), NULL, NULL }
#define EDITLEXER_HOLE(name, styles) StyleTheme_Default, false, true, 0, COUNTOF(styles), CSTRLEN(name), (name), NULL, NULL
#define EDITLEXER_TEXT(name, styles) StyleTheme_Default, false, false, 0, COUNTOF(styles), CSTRLEN(name), (name), NULL, NULL

#define NP2StyleX_MarginLineNumber EDITSTYLE_HOLE(MarginLineNumber, L"Margin and Line Number")
#define NP2StyleX_MatchingBrace EDITSTYLE_HOLE(MatchingBrace, L"Matching Brace")
Expand Down
3 changes: 1 addition & 2 deletions src/EditLexers/stlABAQUS.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ static EDITSTYLE Styles_ABAQUS[] = {
EDITLEXER lexABAQUS = {
SCLEX_APDL, NP2LEX_ABAQUS,
//Settings++Autogenerated -- start of section automatically generated
{
LexerAttr_NoBlockComment,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0) | (1 << 1), // level1, level2
Expand All @@ -50,7 +49,7 @@ EDITLEXER lexABAQUS = {
SCE_APDL_OPERATOR, 0
, KeywordAttr32(0, KeywordAttr_PreSorted) // code folding
| KeywordAttr32(3, KeywordAttr_PreSorted) // star command
},
, SCE_APDL_COMMENT,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"ABAQUS", Styles_ABAQUS),
L"inp",
Expand Down
3 changes: 1 addition & 2 deletions src/EditLexers/stlAPDL.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ static EDITSTYLE Styles_APDL[] = {
EDITLEXER lexAPDL = {
SCLEX_APDL, NP2LEX_APDL,
//Settings++Autogenerated -- start of section automatically generated
{
LexerAttr_NoBlockComment,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0) | (1 << 1), // level1, level2
Expand All @@ -50,7 +49,7 @@ EDITLEXER lexAPDL = {
SCE_APDL_OPERATOR, 0
, KeywordAttr32(0, KeywordAttr_PreSorted) // code folding
| KeywordAttr32(3, KeywordAttr_PreSorted) // star command
},
, SCE_APDL_COMMENT,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"ANSYS APDL", Styles_APDL),
L"cdb",
Expand Down
3 changes: 1 addition & 2 deletions src/EditLexers/stlActionScript.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ static EDITSTYLE Styles_AS[] = {
EDITLEXER lexActionScript = {
SCLEX_JAVASCRIPT, NP2LEX_ACTIONSCRIPT,
//Settings++Autogenerated -- start of section automatically generated
{
LexerAttr_AngleBracketGeneric,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0) | (1 << 1) | (1 << 2), // class, anonymous object, method
Expand All @@ -91,7 +90,7 @@ EDITLEXER lexActionScript = {
| KeywordAttr64(8, KeywordAttr_NoLexer) // function
| KeywordAttr64(9, KeywordAttr_NoLexer) // properties
| KeywordAttr64(10, KeywordAttr_NoLexer) // doc tag
},
, SCE_JS_TASKMARKER,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"ActionScript", Styles_AS),
L"as",
Expand Down
3 changes: 1 addition & 2 deletions src/EditLexers/stlAsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ static EDITSTYLE Styles_ASM[] = {
EDITLEXER lexASM = {
SCLEX_ASM, NP2LEX_ASM,
//Settings++Autogenerated -- start of section automatically generated
{
LexerAttr_Default,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0) | (1 << 1), // level1, level2
Expand All @@ -186,7 +185,7 @@ EDITLEXER lexASM = {
0, 0,
SCE_ASM_OPERATOR, 0,
KeywordAttr_Default
},
, SCE_ASM_TASKMARKER,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"Assembler Source", Styles_ASM),
L"asm; s; sx; inc; cod; a51",
Expand Down
3 changes: 1 addition & 2 deletions src/EditLexers/stlAsymptote.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ static EDITSTYLE Styles_ASY[] = {
EDITLEXER lexAsymptote = {
SCLEX_ASYMPTOTE, NP2LEX_ASYMPTOTE,
//Settings++Autogenerated -- start of section automatically generated
{
LexerAttr_Default,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0) | (1 << 1), // struct, function
Expand All @@ -83,7 +82,7 @@ EDITLEXER lexAsymptote = {
| KeywordAttr32(2, KeywordAttr_PreSorted) // structs
| KeywordAttr32(3, KeywordAttr_PreSorted) // constants
| KeywordAttr32(4, KeywordAttr_NoLexer) // functions
},
, SCE_ASY_TASKMARKER,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"Asymptote Code", Styles_ASY),
L"asy",
Expand Down
3 changes: 1 addition & 2 deletions src/EditLexers/stlAutoHotkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ static EDITSTYLE Styles_AHK[] = {
EDITLEXER lexAutoHotkey = {
SCLEX_AUTOHOTKEY, NP2LEX_AUTOHOTKEY,
//Settings++Autogenerated -- start of section automatically generated
{
LexerAttr_PrintfFormatSpecifier,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0) | (1 << 1), // class, function
Expand All @@ -176,7 +175,7 @@ EDITLEXER lexAutoHotkey = {
| KeywordAttr32(5, KeywordAttr_MakeLower | KeywordAttr_PreSorted) // keys
| KeywordAttr32(6, KeywordAttr_MakeLower | KeywordAttr_PreSorted) // functions
| KeywordAttr64(7, KeywordAttr_NoLexer) // misc
},
, SCE_AHK_TASKMARKER,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"AutoHotkey Script", Styles_AHK),
L"ahk; ia; scriptlet",
Expand Down
3 changes: 1 addition & 2 deletions src/EditLexers/stlAutoIt3.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ static EDITSTYLE Styles_AU3[] = {
EDITLEXER lexAutoIt3 = {
SCLEX_AUTOIT3, NP2LEX_AUTOIT3,
//Settings++Autogenerated -- start of section automatically generated
{
LexerAttr_Default,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0) | (1 << 1), // level1, level2
Expand All @@ -306,7 +305,7 @@ EDITLEXER lexAutoIt3 = {
| KeywordAttr64(7, KeywordAttr_NoLexer) // user defined functions 2
| KeywordAttr64(8, KeywordAttr_NoLexer) // user defined functions 3
| KeywordAttr64(9, KeywordAttr_NoLexer) // misc
},
, SCE_AU3_COMMENTBLOCK,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"AutoIt3 Script", Styles_AU3),
L"au3",
Expand Down
3 changes: 1 addition & 2 deletions src/EditLexers/stlAviSynth.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ static EDITSTYLE Styles_AVS[] = {
EDITLEXER lexAviSynth = {
SCLEX_AVISYNTH, NP2LEX_AVISYNTH,
//Settings++Autogenerated -- start of section automatically generated
{
LexerAttr_Default,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0), // function
Expand All @@ -189,7 +188,7 @@ EDITLEXER lexAviSynth = {
| KeywordAttr32(3, KeywordAttr_MakeLower | KeywordAttr_PreSorted) // external filters
| KeywordAttr32(4, KeywordAttr_MakeLower | KeywordAttr_PreSorted) // properties
| KeywordAttr32(5, KeywordAttr_NoLexer) // options
},
, SCE_AVS_TASKMARKER,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"AviSynth Script", Styles_AVS),
L"avs; avsi",
Expand Down
Loading

0 comments on commit 3ff3623

Please sign in to comment.