/*---------------------------------------------------------
 * Copyright (C) Microsoft Corporation. All rights reserved.
 *--------------------------------------------------------*/
"use strict";define("vs/languages/typescript/common/participants/filenameSuggestions",["require","exports","vs/base/common/uri","vs/base/common/strings","vs/base/common/paths","vs/base/common/collections","vs/base/common/winjs.base","vs/languages/typescript/common/lib/typescriptServices","vs/languages/typescript/common/project/projectService"],function(e,t,n,r,i,a,o,s,c){var l=function(){function e(){}return e.prototype.makeModulePath=function(e,t,n){return e?this._toRequirePath(t,n):this._toReferencesPath(t,n)},e.prototype._toRequirePath=function(e,t){var n,a=i.dirname(t);return n=i.join(i.relative(a,i.dirname(e)),i.basename(e)),r.startsWith(n,"..")||(n="./"+n),n=r.rtrim(n,".ts"),n=r.rtrim(n,".js")},e.prototype._toReferencesPath=function(e,t){var n=i.dirname(t);return i.join(i.relative(n,i.dirname(e)),i.basename(e))},e}();t.PathMaker=l;var u=function(){function e(e){this._resourceServices=e.resourceService}return e.prototype.suggest=function(e,t,i){if(!(i instanceof c.ProjectService))return o.TPromise.as(null);var s=i.getProject(e),u=s.languageService.getSourceFile(e.toString()),d=this._findCurrentWord(e,t,u);if(!d)return o.TPromise.as(null);var p=new l,m=/^['"]/.test(d.wordUntilPosition)?d.wordUntilPosition[0]:'"',h={currentWord:d.wordUntilPosition,suggestions:[]},f=Object.create(null);return i.scriptsNames().forEach(function(t){if(!(d.external&&r.endsWith(t,".d.ts")||t===e.toString()||f[t]||"file"!==n.parse(t).scheme)){var i=p.makeModulePath(d.external,t,e.toString());f[t]={label:r.format("{0}{1}{0}",m,i),codeSnippet:r.rtrim(r.format("{0}{1}{0}",m,i),d.wordAfterPosition),documentationLabel:n.parse(t).fsPath,type:"reference"}}}),h.suggestions=a.values(f),o.TPromise.as(h)},e.prototype._findCurrentWord=function(t,n,r){var i=this._resourceServices.get(t),a=i.getOffsetFromPosition(n),o=s.findTokenOnLeftOfPosition(r,a),c=i.getLineContent(n.lineNumber),l=e._TrippleSlashReference.exec(c);if(l){var u=i.getOffsetFromPosition({lineNumber:n.lineNumber,column:1});return{wordUntilPosition:l[2].substring(0,a-(u+l[1].length)),wordAfterPosition:l[2].substring(a-(u+l[1].length)),external:!1}}if(d.isTokenPath(o,8,160))return{wordUntilPosition:s.getTextOfNode(o).substr(0,a-o.getStart()),wordAfterPosition:s.getTextOfNode(o).substr(a-o.getStart()),external:!0};if(d.isTokenPath(o,8,156,160)){var p=d.parent(o,2);if("require"===s.getTextOfNode(p.expression)||"define"===s.getTextOfNode(p.expression))return{wordUntilPosition:s.getTextOfNode(o).substr(0,a-o.getStart()),wordAfterPosition:s.getTextOfNode(o).substr(a-o.getStart()),external:!0}}},e._TrippleSlashReference=/^(\/\/\/\s*<reference\s+path=)([^\s>]+)/i,e}();t.FilenameSuggestions=u;var d;!function(e){function t(e,t){for(;t-->0&&e;)e=e.parent;return e}function n(e){for(var t=[],n=1;n<arguments.length;n++)t[n-1]=arguments[n];for(;t.length>0&&e;){var r=t.shift();if(r!==e.kind)return!1;e=e.parent}return!0}e.parent=t,e.isTokenPath=n}(d||(d={}))});