@@ -442,6 +442,12 @@ private void addSnippetCompletions(CompletionResultSet result, String prefix) {
442442 .withTypeText ("Function Definition" )
443443 .withIcon (com .intellij .icons .AllIcons .Nodes .Function )
444444 .withInsertHandler ((context , item ) -> {
445+ // 先删除已经插入的 "function" 文本
446+ int startOffset = context .getStartOffset ();
447+ int endOffset = startOffset + "function" .length ();
448+ context .getDocument ().deleteString (startOffset , endOffset );
449+
450+ // 插入完整的函数定义代码片段
445451 String snippet = "function ${1:functionName}(${2:parameters}) {\n ${3:// function body}\n return ${4:value};\n }" ;
446452 insertSnippet (context , snippet );
447453 })
@@ -455,6 +461,12 @@ private void addSnippetCompletions(CompletionResultSet result, String prefix) {
455461 .withTypeText ("If Statement" )
456462 .withIcon (com .intellij .icons .AllIcons .Nodes .Static )
457463 .withInsertHandler ((context , item ) -> {
464+ // 先删除已经插入的 "if" 文本
465+ int startOffset = context .getStartOffset ();
466+ int endOffset = startOffset + "if" .length ();
467+ context .getDocument ().deleteString (startOffset , endOffset );
468+
469+ // 插入完整的 if 语句代码片段
458470 String snippet = "if (${1:condition}) {\n ${2:// code}\n }" ;
459471 insertSnippet (context , snippet );
460472 })
@@ -468,6 +480,12 @@ private void addSnippetCompletions(CompletionResultSet result, String prefix) {
468480 .withTypeText ("For Loop" )
469481 .withIcon (com .intellij .icons .AllIcons .Nodes .Static )
470482 .withInsertHandler ((context , item ) -> {
483+ // 先删除已经插入的 "for" 文本
484+ int startOffset = context .getStartOffset ();
485+ int endOffset = startOffset + "for" .length ();
486+ context .getDocument ().deleteString (startOffset , endOffset );
487+
488+ // 插入完整的 for 循环代码片段
471489 String snippet = "for (${1:initialization}; ${2:condition}; ${3:increment}) {\n ${4:// code}\n }" ;
472490 insertSnippet (context , snippet );
473491 })
@@ -481,6 +499,12 @@ private void addSnippetCompletions(CompletionResultSet result, String prefix) {
481499 .withTypeText ("While Loop" )
482500 .withIcon (com .intellij .icons .AllIcons .Nodes .Static )
483501 .withInsertHandler ((context , item ) -> {
502+ // 先删除已经插入的 "while" 文本
503+ int startOffset = context .getStartOffset ();
504+ int endOffset = startOffset + "while" .length ();
505+ context .getDocument ().deleteString (startOffset , endOffset );
506+
507+ // 插入完整的 while 循环代码片段
484508 String snippet = "while (${1:condition}) {\n ${2:// code}\n }" ;
485509 insertSnippet (context , snippet );
486510 })
@@ -494,6 +518,12 @@ private void addSnippetCompletions(CompletionResultSet result, String prefix) {
494518 .withTypeText ("Class Definition" )
495519 .withIcon (com .intellij .icons .AllIcons .Nodes .Class )
496520 .withInsertHandler ((context , item ) -> {
521+ // 先删除已经插入的 "class" 文本
522+ int startOffset = context .getStartOffset ();
523+ int endOffset = startOffset + "class" .length ();
524+ context .getDocument ().deleteString (startOffset , endOffset );
525+
526+ // 插入完整的类定义代码片段
497527 String snippet = "class ${1:ClassName} {\n ${2:// class body}\n }" ;
498528 insertSnippet (context , snippet );
499529 })
@@ -505,18 +535,25 @@ private void addSnippetCompletions(CompletionResultSet result, String prefix) {
505535 * 插入代码片段
506536 */
507537 private void insertSnippet (InsertionContext context , String snippet ) {
508- // 简单的代码片段实现 ,将 ${n} 替换为占位符
538+ // 处理代码片段 ,将 ${n:placeholder} 替换为占位符文本
509539 String processedSnippet = snippet .replaceAll ("\\ $\\ {([0-9]+):([^}]*)\\ }" , "$2" );
540+ // 移除剩余的 ${n} 占位符(没有默认值的)
510541 processedSnippet = processedSnippet .replaceAll ("\\ $\\ {([0-9]+)\\ }" , "" );
511542
512543 // 插入文本
513544 context .getDocument ().insertString (context .getStartOffset (), processedSnippet );
514545
515- // 将光标移动到第一个占位符位置
516- int firstPlaceholder = processedSnippet .indexOf ("${1" );
517- if (firstPlaceholder >= 0 ) {
518- int offset = context .getStartOffset () + firstPlaceholder ;
519- context .getEditor ().getCaretModel ().moveToOffset (offset );
546+ // 查找第一个占位符并定位光标
547+ String [] placeholders = {"functionName" , "ClassName" , "condition" , "initialization" , "parameters" };
548+ for (String placeholder : placeholders ) {
549+ int placeholderIndex = processedSnippet .indexOf (placeholder );
550+ if (placeholderIndex >= 0 ) {
551+ int offset = context .getStartOffset () + placeholderIndex ;
552+ context .getEditor ().getCaretModel ().moveToOffset (offset );
553+ // 选中占位符文本,方便用户直接输入
554+ context .getEditor ().getSelectionModel ().setSelection (offset , offset + placeholder .length ());
555+ break ;
556+ }
520557 }
521558 }
522559 }
0 commit comments