Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

55
LINES

< > BotCompany Repo | #1034700 // HashRefTokenMaker - for auto-completion [dev.]

JavaX fragment (include) [tags: use-pretranspiled]

Uses 1485K of libraries. Click here for Pure Java version (5279L/29K).

1  
// I think tokens should not cross lines, it messes about
2  
// RSyntaxTextArea's hyperlink finding.
3  
4  
please include class RSyntaxTextArea.
5  
6  
import org.fife.ui.rsyntaxtextarea.TokenMakerBase;
7  
8  
sclass HashRefTokenMaker extends TokenMakerBase {
9  
  delegate Token to org.fife.ui.rsyntaxtextarea.
10  
  
11  
  /**
12  
   * Returns a list of tokens representing the given text.
13  
   *
14  
   * @param text The text to break into tokens.
15  
   * @param startTokenType The token with which to start tokenizing.
16  
   * @param startOffset The offset at which the line of tokens begins.
17  
   * @return A linked list of tokens representing <code>text</code>.
18  
   */
19  
  @Override
20  
  public Token getTokenList(Segment text, int startTokenType, final int startOffset) {
21  
    resetTokenList();
22  
23  
    char[] array = text.array;
24  
    int offset = text.offset;
25  
    int count = text.count;
26  
    int end = offset + count;
27  
    int newStartOffset = startOffset - offset;
28  
    S src = str(array, offset, count);
29  
    LS tok = hashRefTok(src);
30  
31  
    int ofs = offset;
32  
    for i, t over tok: {
33  
      if (empty(t)) continue;
34  
      bool isHyperlink = odd(i);
35  
      
36  
      // split at line breaks, spaces and punctuation to allow
37  
      // word wrapping (does it work?)
38  
      for (part : singlePredTok(c -> isSpaceEtc(c), t)) {
39  
        if (nempty(part)) {
40  
          var tokenType = isHyperlink ? Token.VARIABLE
41  
            : startsWithSpaceEtc(part) ? Token.WHITESPACE
42  
            : Token.IDENTIFIER;
43  
          int l = l(part);
44  
          addToken(array, ofs, ofs+l-1, tokenType, ofs+newStartOffset, isHyperlink);
45  
          ofs += l;
46  
        }
47  
      }
48  
    }
49  
50  
    addNullToken();
51  
52  
    // Return the first token in our linked list.
53  
    ret firstToken;
54  
  }
55  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034700
Snippet name: HashRefTokenMaker - for auto-completion [dev.]
Eternal ID of this version: #1034700/14
Text MD5: 9872009dfa08aad7598c07307acd7cf9
Transpilation MD5: 049903486f6a0b3139e5c1c60b286987
Author: stefan
Category: javax / tokenizing
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-06 18:48:26
Source code size: 1742 bytes / 55 lines
Pitched / IR pitched: No / No
Views / Downloads: 107 / 234
Version history: 13 change(s)
Referenced in: [show references]