2013-07-25 2 views
2

나는 codemirror autocomplete demo를 사용하고 있습니다. 그것은 응용 프로그램 캐시, defaultStatus 및 framenet 및 더 많은 제안과 같은 몇 가지 자바 스크립트 키워드를 보여줍니다. 키워드로 추천 키워드를 표시하고 싶습니다. 그러나 나는이 javascript 키워드가 올 소스를 찾을 수 없습니다. 이걸 도와주세요.Codemirror 자동 완성 - 제안 소스

답변

3

자바 스크립트 모드는 매우 이해하기 힘듭니다. 그래서 나는 파이썬 모드를 사용했고 python-hint.js를 변경하는 것은 매우 쉽고 내가 원하는 것을 출력했다. 감사합니다 Eliran ..

+0

프로세스를 단순화하는 위대한 생각, 주도권을 계속하는 방법. –

2

소스에서 볼 수있는 것처럼 몇 가지 키워드가 포함 된 javascript-hint.js을 확인하십시오. 예 : 자바 스크립트 키워드 (라인 96) : 가야

var javascriptKeywords = 
    ("break case catch continue debugger default delete do else false finally for function " + 
    "if in instanceof new null return switch throw true try typeof var void while with") 
    .split(" "); 

당신은 당신의 자신의 *-hint.js 파일을 작성하는 시작했다.

+1

는 사실이이 파일에 거기있는 다른 키워드의 많은 그 중 몇 가지 키워드입니다 체인. –

+2

글쎄, 내가 말 했잖아. * 시작해야 해. *. 데모 코드를 살펴 보는 것만으로도 충분합니다. ** 소스를 사용하십시오, 루크! ** –

1

나는 실제로이 문제를 발견했을 때 이전 CodeMirror 자동 완성 질문을보고있었습니다.

Javascript 완료는 소스의 조합에서 비롯되었으며 Eliran이 javascript-hint.js을 지적한대로 소스에서이를 확인할 수 있습니다.

여기
function getCompletions(token, context, keywords, options) { 
    var found = [], start = token.string, global = options && options.globalScope || window; 
    function maybeAdd(str) { 
     if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str); 
    } 
    function gatherCompletions(obj) { 
     if (typeof obj == "string") forEach(stringProps, maybeAdd); 
     else if (obj instanceof Array) forEach(arrayProps, maybeAdd); 
     else if (obj instanceof Function) forEach(funcProps, maybeAdd); 
     for (var name in obj) maybeAdd(name); 
    } 

    if (context && context.length) { 
     // If this is a property, see if it belongs to some object we can 
     // find in the current environment. 
     var obj = context.pop(), base; 
     if (obj.type && obj.type.indexOf("variable") === 0) { 
     if (options && options.additionalContext) 
      base = options.additionalContext[obj.string]; 
     if (!options || options.useGlobalScope !== false) 
      base = base || global[obj.string]; 
     } else if (obj.type == "string") { 
     base = ""; 
     } else if (obj.type == "atom") { 
     base = 1; 
     } else if (obj.type == "function") { 
     if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') && 
      (typeof global.jQuery == 'function')) 
      base = global.jQuery(); 
     else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function')) 
      base = global._(); 
     } 
     while (base != null && context.length) 
     base = base[context.pop().string]; 
     if (base != null) gatherCompletions(base); 
    } else { 
     // If not, just look in the global object and any local scope 
     // (reading into JS mode internals to get at the local and global variables) 
     for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name); 
     for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name); 
     if (!options || options.useGlobalScope !== false) 
     gatherCompletions(global); 
     forEach(keywords, maybeAdd); 
    } 
    return found; 
    } 

몇 소스를 볼 수 있습니다

이 관련이 시간에 기능입니다. 키워드은 (미도시)를 호출하는 기능에 의해 제공되는 파라미터이며,이 경우에는 Eliran 언급 분할 문자열이었다 if (context && context.length) {

에서 : 여기에 논리에서 분기

var javascriptKeywords = ("break case catch continue debugger default delete do else false finally for function " + 
       "if in instanceof new null return switch throw true try typeof var void while with").split(" "); 

존재는 호출자가 전달한 컨텍스트 개체 (이 경우 속성 체인)를 사용하는 실제 사례입니다. additionalContext 개체를 사용할 수 있습니다. 또는 useGlobalScope가 옵션에서 명시 적으로 해제되어 있지 않으면 전역 개체를 기준으로 사용하여 조회를 시작할 수 있습니다 (문자열, 원자 및 함수 유형에는 특수 처리 논리가 있음). 그런 다음 while 루프를 사용하여 컨텍스트 체인을 해결하고 gatherCompletions을 호출합니다.

var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " + 
        "toUpperCase toLowerCase split concat match replace search").split(" "); 
    var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " + 
        "lastIndexOf every some filter forEach map reduce reduceRight ").split(" "); 
    var funcProps = "prototype apply call bind".split(" "); 

를 또는 자바 스크립트 객체 자체의 속성을 반복하여 : gatherCompletions에 전달 된 객체의 유형에 따라 그것은 분할 문자열에 의해 제공되는 다른 하드 코딩 된 목록을 추가 할 수 있습니다.

마지막으로 다른 사람의 경우 token.state.localVars, token.state.globalVars, 다시 명시 적으로 글로벌 개체가 gatherCompletions 함수의 객체로 사용할 해제하지 않을 경우. 여기에서 위에서 언급 한 키워드를 확인할 수 있습니다. global은 에 제공되거나 window 값을 가정합니다.요약

그냥 빨리 여기에 모든 소스 보여

:

  1. javascriptKeywords을
  2. funcProps
  3. arrayProps
  4. stringProps
  5. 전역 객체
  6. options.additionalContext
  7. token.state.localVars
  8. token.state.globalVars는
  9. 컨텍스트 속성은

+0

정확히'options'는 무엇입니까? – dv02

+0

나는 그것들이 모두 문서화되어 있다고는 생각하지 않지만 [여기] (https://codemirror.net/doc/manual.html#config)를 볼 수 있습니다. 이 경우 위의 코드에서 "additionalContext"가 암시됩니다 :'base = options.additionalContext [obj.string];'. 나는'obj.string' 값이 무엇일 지 내 머리 꼭대기에서 떠 올릴 수는 없지만 휴식을 취하고 그것을 조사 할 수는 있습니다. –

+0

전체 기능에 옵션이 없으므로 검사 할 수 없습니다. python-hint.js와 show-hint.js에서 단어를 한 번도 찾을 수 없습니다. – dv02