Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/codemirror/addon/hint/python-hint.js
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45// CodeMirror, copyright (c) by Marijn Haverbeke and others6// Distributed under an MIT license: http://codemirror.net/LICENSE78(function(mod) {9if (typeof exports == "object" && typeof module == "object") // CommonJS10mod(require("codemirror"));11else if (typeof define == "function" && define.amd) // AMD12define(["../../lib/codemirror"], mod);13else // Plain browser env14mod(CodeMirror);15})(function(CodeMirror) {16"use strict";1718function forEach(arr, f) {19for (var i = 0, e = arr.length; i < e; ++i) f(arr[i]);20}2122function arrayContains(arr, item) {23if (!Array.prototype.indexOf) {24var i = arr.length;25while (i--) {26if (arr[i] === item) {27return true;28}29}30return false;31}32return arr.indexOf(item) != -1;33}3435function scriptHint(editor, _keywords, getToken) {36// Find the token at the cursor37var cur = editor.getCursor(), token = getToken(editor, cur), tprop = token;38// If it's not a 'word-style' token, ignore the token.3940if (!/^[\w$_]*$/.test(token.string)) {41token = tprop = {start: cur.ch, end: cur.ch, string: "", state: token.state,42className: token.string == ":" ? "python-type" : null};43}4445if (!context) var context = [];46context.push(tprop);4748var completionList = getCompletions(token, context);49completionList = completionList.sort();5051return {list: completionList,52from: CodeMirror.Pos(cur.line, token.start),53to: CodeMirror.Pos(cur.line, token.end)};54}5556function pythonHint(editor) {57return scriptHint(editor, pythonKeywordsU, function (e, cur) {return e.getTokenAt(cur);});58}59CodeMirror.registerHelper("hint", "python", pythonHint);6061var pythonKeywords = "and del from not while as elif global or with assert else if pass yield"62+ "break except import print class exec in raise continue finally is return def for lambda try";63var pythonKeywordsL = pythonKeywords.split(" ");64var pythonKeywordsU = pythonKeywords.toUpperCase().split(" ");6566var pythonBuiltins = "abs divmod input open staticmethod all enumerate int ord str "67+ "any eval isinstance pow sum basestring execfile issubclass print super"68+ "bin file iter property tuple bool filter len range type"69+ "bytearray float list raw_input unichr callable format locals reduce unicode"70+ "chr frozenset long reload vars classmethod getattr map repr xrange"71+ "cmp globals max reversed zip compile hasattr memoryview round __import__"72+ "complex hash min set apply delattr help next setattr buffer"73+ "dict hex object slice coerce dir id oct sorted intern ";74var pythonBuiltinsL = pythonBuiltins.split(" ").join("() ").split(" ");75var pythonBuiltinsU = pythonBuiltins.toUpperCase().split(" ").join("() ").split(" ");7677function getCompletions(token, context) {78var found = [], start = token.string;79function maybeAdd(str) {80if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);81}8283function gatherCompletions(_obj) {84forEach(pythonBuiltinsL, maybeAdd);85forEach(pythonBuiltinsU, maybeAdd);86forEach(pythonKeywordsL, maybeAdd);87forEach(pythonKeywordsU, maybeAdd);88}8990if (context) {91// If this is a property, see if it belongs to some object we can92// find in the current environment.93var obj = context.pop(), base;9495if (obj.type == "variable")96base = obj.string;97else if(obj.type == "variable-3")98base = ":" + obj.string;99100while (base != null && context.length)101base = base[context.pop().string];102if (base != null) gatherCompletions(base);103}104return found;105}106});107108109