text:
import './base-imports';

import { autoBindMethodsForReact } from 'class-autobind-decorator';
import classnames from 'classnames';
import clone from 'clone';
import CodeMirror, { CodeMirrorLinkClickCallback, EditorConfiguration, ShowHintOptions } from 'codemirror';
import { GraphQLInfoOptions } from 'codemirror-graphql/info';
import { ModifiedGraphQLJumpOptions } from 'codemirror-graphql/jump';
import deepEqual from 'deep-equal';
import { json as jsonPrettify } from 'insomnia-prettify';
import { query as queryXPath } from 'insomnia-xpath';
import { JSONPath } from 'jsonpath-plus';
import React, { Component, CSSProperties, forwardRef, ForwardRefRenderFunction, ReactNode } from 'react';
import { useSelector } from 'react-redux';
import { unreachable } from 'ts-assert-unreachable';
import vkBeautify from 'vkbeautify';
import zprint from 'zprint-clj';

import {
  AUTOBIND_CFG,
  DEBOUNCE_MILLIS,
  EditorKeyMap,
  isMac,
} from '../../../common/constants';
import { hotKeyRefs } from '../../../common/hotkeys';
import { executeHotKey } from '../../../common/hotkeys-listener';
import { keyboardKeys as keyCodes } from '../../../common/keyboard-keys';
import * as misc from '../../../common/misc';
import { getTagDefinitions } from '../../../templating/index';
import { NunjucksParsedTag } from '../../../templating/utils';
import { useGatedNunjucks } from '../../context/nunjucks/use-gated-nunjucks';
import { selectSettings } from '../../redux/selectors';
import { Dropdown } from '../base/dropdown/dropdown';
import { DropdownButton } from '../base/dropdown/dropdown-button';
import { DropdownItem } from '../base/dropdown/dropdown-item';
import { KeydownBinder } from '../keydown-binder';
import { FilterHelpModal } from '../modals/filter-help-modal';
import { showModal } from '../modals/index';
import { normalizeIrregularWhitespace } from './normalizeIrregularWhitespace';

const TAB_SIZE = 4;
const MAX_SIZE_FOR_LINTING = 1000000; // Around 1MB

// Global object used for storing and persisting editor states
const editorStates = {};
const BASE_CODEMIRROR_OPTIONS: CodeMirror.EditorConfiguration = {
  lineNumbers: true,
  placeholder: 'Start Typing...',
  foldGutter: true,
  autoRefresh: { delay: 2000 },
  lineWrapping: true,
  scrollbarStyle: 'native',
  lint: true,
  matchBrackets: true,
  autoCloseBrackets: true,
  tabSize: TAB_SIZE,
  indentUnit: TAB_SIZE,
  hintOptions: undefined,
  dragDrop: true,
  viewportMargin: 30,
  // default 10
  selectionPointer: 'default',
  styleActiveLine: true,
  indentWithTabs: true,
  showCursorWhenSelecting: false,
  cursorScrollMargin: 12,
  // NOTE: This is px
  keyMap: 'default',
  extraKeys: CodeMirror.normalizeKeyMap({
    'Ctrl-Q': function(cm) {
      cm.foldCode(cm.getCursor());
    },
    [isMac() ? 'Cmd-Enter' : 'Ctrl-Enter']: function() {
      // HACK: So nothing conflicts withe the "Send Request" shortcut
    },
    [isMac() ? 'Cmd-/' : 'Ctrl-/']: 'toggleComment',
    // Autocomplete
    'Ctrl-Space': 'autocomplete',
    // Change default find command from "find" to "findPersistent" so the
    // search box stays open after pressing Enter
    [isMac() ? 'Cmd-F' : 'Ctrl-F']: 'findPersistent',
    [isMac() ? 'Shift-Cmd--' : 'Shift-Ctrl--']: 'foldAll',
    [isMac() ? 'Shift-Cmd-=' : 'Shift-Ctrl-=']: 'unfoldAll',
    'Shift-Tab': 'indentLess',
    Tab: 'indentMore',
  }),
  // NOTE: Because the lint mode is initialized immediately, the lint gutter needs to
  //   be in the default options. DO NOT REMOVE THIS.
  gutters: ['CodeMirror-lint-markers'],
};

export type CodeEditorOnChange = (value: string) => void;

interface RawProps {
  onChange?: CodeEditorOnChange;
  onCursorActivity?: (cm: CodeMirror.EditorFromTextArea) => void;
  onFocus?: (e: FocusEvent) => void;
  onBlur?: (e: FocusEvent) => void;
  onClickLink?: CodeMirrorLinkClickCallback;
  onKeyDown?: (e: KeyboardEvent, value: string) => void;
  onMouseLeave?: React.MouseEventHandler<HTMLDivElement>;
  onClick?: React.MouseEventHandler<HTMLDivElement>;
  onPaste?: (e: ClipboardEvent) => void;
  onCodeMirrorInit?: (editor: CodeMirror.EditorFromTextArea) => void;
  getAutocompleteConstants?: () => string[] | PromiseLike<string[]>;
  getAutocompleteSnippets?: () => CodeMirror.Snippet[];
  mode?: string;
  id?: string;
  placeholder?: string;
  hideLineNumbers?: boolean;
  hideGutters?: boolean;
  noMatchBrackets?: boolean;
  hideScrollbars?: boolean;
  defaultValue?: string;
  tabIndex?: number;
  autoPrettify?: boolean;
  manualPrettify?: boolean;
  noLint?: boolean;
  noDragDrop?: boolean;
  noStyleActiveLine?: boolean;
  className?: string;
  style?: Object;
  updateFilter?: (filter: string) => void;
  defaultTabBehavior?: boolean;
  readOnly?: boolean;
  type?: string;
  filter?: string;
  filterHistory?: string[];
  singleLine?: boolean;
  debounceMillis?: number;
  dynamicHeight?: boolean;
  autoCloseBrackets?: boolean;
  hintOptions?: ShowHintOptions;
  lintOptions?: any;
  infoOptions?: GraphQLInfoOptions;
  jumpOptions?: ModifiedGraphQLJumpOptions;
  uniquenessKey?: string;
  raw?: boolean;
}

interface FCProps {
  enableNunjucks?: boolean;
  ignoreEditorFontSettings?: boolean;
}

const useDerivedProps = ({ enableNunjucks, ignoreEditorFontSettings }: FCProps) => {
  const {
    handleRender,
    handleGetRenderContext,
  } = useGatedNunjucks({ disabled: !enableNunjucks });

  const {
    hotKeyRegistry,
    autocompleteDelay,
    editorFontSize,
    editorIndentSize,
    editorKeyMap,
    editorLineWrapping,
    editorIndentWithTabs,
    nunjucksPowerUserMode,
    showVariableSourceAndValue,
  } = useSelector(selectSettings);

  return {
    render: handleRender,
    getRenderContext: handleGetRenderContext,
    hotKeyRegistry,
    autocompleteDelay,
    fontSize: ignoreEditorFontSettings ? undefined : editorFontSize,
    indentSize: ignoreEditorFontSettings ? undefined : editorIndentSize,
    keyMap: editorKeyMap,
    lineWrapping: ignoreEditorFontSettings ? undefined : editorLineWrapping,
    indentWithTabs: ignoreEditorFontSettings ? undefined : editorIndentWithTabs,
    nunjucksPowerUserMode,
    showVariableSourceAndValue,
  };
};

const CodeEditorFCWithRef: ForwardRefRenderFunction<UnconnectedCodeEditor, RawProps & FCProps> = (
  { enableNunjucks, ignoreEditorFontSettings, ...rawProps },
  ref
) => {
  const derivedProps = useDerivedProps({ enableNunjucks, ignoreEditorFontSettings });

  return <UnconnectedCodeEditor
    ref={ref}
    {...rawProps}
    {...derivedProps}
  />;
};

export const CodeEditor = forwardRef(CodeEditorFCWithRef);

type Props = RawProps & ReturnType<typeof useDerivedProps>;

interface State {
  filter: string;
}

function isMarkerRange(mark?: CodeMirror.Position | CodeMirror.MarkerRange): mark is CodeMirror.MarkerRange {
  if (!mark) {
    return false;
  }

  return Object.prototype.hasOwnProperty.call(mark, 'from');
}

@autoBindMethodsForReact(AUTOBIND_CFG)
export class UnconnectedCodeEditor extends Component<Props, State> {
  private _uniquenessKey?: string;
  private _previousUniquenessKey?: string;
  private _originalCode: string;
  codeMirror?: CodeMirror.EditorFromTextArea;
  private _filterInput: HTMLInputElement;
  private _autocompleteDebounce: NodeJS.Timeout | null = null;
  private _filterTimeout: NodeJS.Timeout | null = null;

  constructor(props: Props) {
    super(props);
    this.state = {
      filter: props.filter || '',
    };
    this._originalCode = '';
    this._uniquenessKey = this.props.uniquenessKey;
    this._previousUniquenessKey = 'n/a';
  }

  componentWillUnmount() {
    if (this.codeMirror) {
      this.codeMirror.toTextArea();
      this.codeMirror.closeHintDropdown();
    }
  }

  // eslint-disable-next-line camelcase
  UNSAFE_componentWillReceiveProps(nextProps: Props) {
    this._uniquenessKey = nextProps.uniquenessKey;
    this._previousUniquenessKey = this.props.uniquenessKey;
    // Sync the filter too
    this.setState({
      filter: nextProps.filter || '',
    });
  }

  componentDidUpdate() {
    this._codemirrorSetOptions();

    const { defaultValue } = this.props;

    if (this._uniquenessKey && this._uniquenessKey !== this._previousUniquenessKey) {
      this._codemirrorSetValue(defaultValue);

      this._restoreState();
    }
  }

  shouldComponentUpdate(nextProps) {
    // Update if any properties changed, except value. We ignore value.
    for (const key of Object.keys(nextProps)) {
      if (key === 'defaultValue') {
        continue;
      }

      if (this.props[key] !== nextProps[key]) {
        return true;
      }
    }

    return false;
  }

  selectAll() {
    if (this.codeMirror) {
      this.codeMirror.setSelection(
        {
          line: 0,
          ch: 0,
        },
        {
          line: this.codeMirror.lineCount(),
          ch: 0,
        },
      );
    }
  }

  focus() {
    if (this.codeMirror) {
      this.codeMirror.focus();
    }
  }

  refresh() {
    if (this.codeMirror) {
      this.codeMirror.refresh();
    }
  }

  setCursor(ch: number, line = 0) {
    if (this.codeMirror) {
      if (!this.hasFocus()) {
        this.focus();
      }

      this.codeMirror.setCursor({
        line,
        ch,
      });
    }
  }

  setSelection(chStart: number, chEnd: number, lineStart: number, lineEnd: number) {
    if (this.codeMirror) {
      this.codeMirror.setSelection(
        {
          line: lineStart,
          ch: chStart,
        },
        {
          line: lineEnd,
          ch: chEnd,
        },
      );
      this.codeMirror.scrollIntoView({
        line: lineStart,
        ch: chStart,
      });
    }
  }

  scrollToSelection(chStart: number, chEnd: number, lineStart: number, lineEnd: number) {
    const selectionFocusPos = window.innerHeight / 2 - 100;

    if (this.codeMirror) {
      this.codeMirror.setSelection(
        {
          line: lineStart,
          ch: chStart,
        },
        {
          line: lineEnd,
          ch: chEnd,
        },
      );
      this.codeMirror.scrollIntoView(
        {
          line: lineStart,
          ch: chStart,
        }, // If sizing permits, position selection just above center
        selectionFocusPos,
      );
    }
  }

  getSelectionStart() {
    if (!this.codeMirror) {
      return null;
    }

    const selections = this.codeMirror.listSelections();

    if (selections.length) {
      return selections[0].anchor.ch;
    } else {
      return 0;
    }
  }

  getSelectionEnd() {
    if (!this.codeMirror) {
      return null;
    }

    const selections = this.codeMirror.listSelections();

    if (selections.length) {
      return selections[0].head.ch;
    } else {
      return 0;
    }
  }

  focusEnd() {
    if (!this.codeMirror) {
      return;
    }

    if (!this.hasFocus()) {
      this.focus();
    }

    const doc = this.codeMirror.getDoc();
    doc.setCursor(doc.lineCount(), 0);
  }

  hasFocus() {
    if (this.codeMirror) {
      return this.codeMirror.hasFocus();
    } else {
      return false;
    }
  }

  setAttribute(name: string, value: string) {
    if (!this.codeMirror) {
      return;
    }

    this.codeMirror.getTextArea().parentElement?.setAttribute(name, value);
  }

  removeAttribute(name: string) {
    if (!this.codeMirror) {
      return;
    }

    this.codeMirror.getTextArea().parentElement?.removeAttribute(name);
  }

  getAttribute(name: string) {
    if (!this.codeMirror) {
      return;
    }

    this.codeMirror.getTextArea().parentElement?.getAttribute(name);
  }

  clearSelection() {
    if (!this.codeMirror) {
      return;
    }

    // Never do this if dropdown is open
    if (this.codeMirror?.isHintDropdownActive()) {
      return;
    }

    this.codeMirror.setSelection(
      {
        line: -1,
        ch: -1,
      },
      {
        line: -1,
        ch: -1,
      },
      {
        scroll: false,
      },
    );
  }

  getValue() {
    if (this.codeMirror) {
      return this.codeMirror.getValue();
    } else {
      return '';
    }
  }

  _persistState() {
    const { uniquenessKey } = this.props;

    if (!uniquenessKey || !this.codeMirror) {
      return;
    }

    const marks = this.codeMirror
      .getAllMarks()
      .filter(mark => mark.__isFold)
      .map(mark => {
        const result = mark.find();

        if (isMarkerRange(result)) {
          return result;
        }

        return {
          from: undefined,
          to: undefined,
        };
      });

    editorStates[uniquenessKey] = {
      scroll: this.codeMirror.getScrollInfo(),
      selections: this.codeMirror.listSelections(),
      cursor: this.codeMirror.getCursor(),
      history: this.codeMirror.getHistory(),
      marks,
    };
  }

  _restoreState() {
    const { uniquenessKey } = this.props;

    if (uniquenessKey === undefined) {
      return;
    }
    if (!editorStates.hasOwnProperty(uniquenessKey)) {
      return;
    }
    if (!this.codeMirror) {
      return;
    }

    const { scroll, selections, cursor, history, marks } = editorStates[uniquenessKey];
    this.codeMirror.scrollTo(scroll.left, scroll.top);
    this.codeMirror.setHistory(history);
    // NOTE: These won't be visible unless the editor is focused
    this.codeMirror.setCursor(cursor.line, cursor.ch, { scroll: false });
    this.codeMirror.setSelections(selections, undefined, { scroll: false });

    // Restore marks one-by-one
    for (const { from, to } of marks || []) {
      this.codeMirror.foldCode(from, to);
    }
  }

  _setFilterInputRef(n: HTMLInputElement) {
    this._filterInput = n;
  }

  _handleInitTextarea(textarea: HTMLTextAreaElement) {
    if (!textarea) {
      // Not mounted
      return;
    }

    if (this.codeMirror) {
      // Already initialized
      return;
    }

    const foldOptions: CodeMirror.EditorConfiguration['foldOptions'] = {
      widget: (from, to) => {
        let count;
        // Get open / close token
        let startToken = '{';
        let endToken = '}';
        // Prevent retrieving an invalid content if undefined
        if (!from?.line || !to?.line) return '\u2194';
        const prevLine = this.codeMirror?.getLine(from.line);
        if (!prevLine) return '\u2194';

        if (prevLine.lastIndexOf('[') > prevLine.lastIndexOf('{')) {
          startToken = '[';
          endToken = ']';
        }

        // Get json content
        const internal = this.codeMirror?.getRange(from, to);
        const toParse = startToken + internal + endToken;

        // Get key count
        try {
          const parsed = JSON.parse(toParse);
          count = Object.keys(parsed).length;
        } catch (e) { }

        return count ? `\u21A4 ${count} \u21A6` : '\u2194';
      },
    };
    const { defaultValue, debounceMillis: ms } = this.props;
    this.codeMirror = CodeMirror.fromTextArea(textarea, {
      ...BASE_CODEMIRROR_OPTIONS,
      foldOptions,
    });
    // Set default listeners
    const debounceMillis = typeof ms === 'number' ? ms : DEBOUNCE_MILLIS;
    this.codeMirror.on('changes', misc.debounce(this._codemirrorValueChanged, debounceMillis));
    this.codeMirror.on('beforeChange', this._codemirrorValueBeforeChange);
    this.codeMirror.on('keydown', this._codemirrorKeyDown);
    this.codeMirror.on('keyup', this._codemirrorTriggerCompletionKeyUp);
    this.codeMirror.on('endCompletion', this._codemirrorEndCompletion);
    this.codeMirror.on('focus', this._codemirrorFocus);
    this.codeMirror.on('blur', this._codemirrorBlur);
    this.codeMirror.on('paste', this._codemirrorPaste);
    this.codeMirror.on('scroll', this._codemirrorScroll);
    this.codeMirror.on('fold', this._codemirrorToggleFold);
    this.codeMirror.on('unfold', this._codemirrorToggleFold);
    this.codeMirror.on('keyHandled', this._codemirrorKeyHandled);
    // Prevent these things if we're type === "password"
    this.codeMirror.on('copy', this._codemirrorPreventWhenTypePassword);
    this.codeMirror.on('cut', this._codemirrorPreventWhenTypePassword);
    this.codeMirror.on('dragstart', this._codemirrorPreventWhenTypePassword);
    this.codeMirror.setCursor({
      line: -1,
      ch: -1,
    });

    let extraKeys = BASE_CODEMIRROR_OPTIONS.extraKeys;
    extraKeys = extraKeys && typeof extraKeys !== 'string' ? extraKeys : {};

    this.codeMirror.setOption('extraKeys', {
      ...extraKeys,
      Tab: cm => {
        // Indent with tabs or spaces
        // From https://github.com/codemirror/CodeMirror/issues/988#issuecomment-14921785
        if (cm.somethingSelected()) {
          cm.indentSelection('add');
        } else {
          cm.replaceSelection(this._indentChars(), 'end');
        }
      },
    });

    // Set editor options
    this._codemirrorSetOptions();

    const setup = () => {
      // Actually set the value
      this._codemirrorSetValue(defaultValue || '');

      // Clear history so we can't undo the initial set
      this.codeMirror?.clearHistory();

      // Setup nunjucks listeners
      // TODO: we shouldn't need to set setup nunjucks if we're in readonly mode
      if (this.props.render && !this.props.nunjucksPowerUserMode) {
        this.codeMirror?.enableNunjucksTags(
          this.props.render,
          this.props.getRenderContext,
          this.props.showVariableSourceAndValue,
        );
      }

      // Make URLs clickable
      if (this.props.onClickLink) {
        this.codeMirror?.makeLinksClickable(this.props.onClickLink);
      }

      // HACK: Refresh because sometimes it renders too early and the scroll doesn't quite fit.
      setTimeout(() => {
        this.codeMirror?.refresh();
      }, 100);

      // Restore the state
      this._restoreState();
    };

    // Do this a bit later for big values so we don't block the render process
    if (defaultValue && defaultValue.length > 10000) {
      setTimeout(setup, 100);
    } else {
      setup();
    }

    if (this.props.onCodeMirrorInit) {
      this.props.onCodeMirrorInit(this.codeMirror);
    }

    // NOTE: Start listening to cursor after everything because it seems to fire
    // immediately for some reason
    this.codeMirror.on('cursorActivity', this._codemirrorCursorActivity);
  }

  static _isJSON(mode?: string) {
    if (!mode) {
      return false;
    }

    return mode.indexOf('json') !== -1;
  }

  static _isYAML(mode?: string) {
    if (!mode) {
      return false;
    }

    return mode.indexOf('yaml') !== -1;
  }

  static _isXML(mode?: string) {
    if (!mode) {
      return false;
    }

    return mode.indexOf('xml') !== -1;
  }

  static _isEDN(mode?: string) {
    if (!mode) {
      return false;
    }

    return mode === 'application/edn' || mode.indexOf('clojure') !== -1;
  }

  _indentChars() {
    return this.codeMirror?.getOption('indentWithTabs')
      ? '\t'
      : new Array((this.codeMirror?.getOption?.('indentUnit') || 0) + 1).join(' ');
  }

  _prettify() {
    const canPrettify = this._canPrettify();
    if (!canPrettify) {
      return;
    }

    const code = this.codeMirror?.getValue();
    this._codemirrorSetValue(code, canPrettify);
  }

  _prettifyJSON(code: string) {
    try {
      let jsonString = code;

      if (this.props.updateFilter && this.state.filter) {
        try {
          const codeObj = JSON.parse(code);
          const results = JSONPath({ json: codeObj, path: this.state.filter.trim() });
          jsonString = JSON.stringify(results);
        } catch (err) {
          console.log('[jsonpath] Error: ', err);
          jsonString = '[]';
        }
      }

      return jsonPrettify(jsonString, this._indentChars(), this.props.autoPrettify);
    } catch (e) {
      // That's Ok, just leave it
      return code;
    }
  }

  static _prettifyEDN(code: string) {
    try {
      return zprint(code, null);
    } catch (e) {
      return code;
    }
  }

  _prettifyXML(code: string) {
    if (this.props.updateFilter && this.state.filter) {
      try {
        const results = queryXPath(code, this.state.filter);
        code = `<result>${results.map(r => r.outer).join('\n')}</result>`;
      } catch (err) {
        // Failed to parse filter (that's ok)
        code = `<error>${err.message}</error>`;
      }
    }

    try {
      return vkBeautify.xml(code, this._indentChars());
    } catch (e) {
      // Failed to parse so just return original
      return code;
    }
  }

  async _handleKeyDown(event: KeyboardEvent) {
    executeHotKey(event, hotKeyRefs.BEAUTIFY_REQUEST_BODY, this._prettify);
  }

  /**
   * Sets options on the CodeMirror editor while also sanitizing them
   */
  async _codemirrorSetOptions() {
    const {
      mode: rawMode,
      autoCloseBrackets,
      autocompleteDelay,
      dynamicHeight,
      getAutocompleteConstants,
      getAutocompleteSnippets,
      getRenderContext,
      hideGutters,
      hideLineNumbers,
      hideScrollbars,
      hintOptions,
      hotKeyRegistry,
      indentSize,
      indentWithTabs,
      infoOptions,
      jumpOptions,
      keyMap,
      lineWrapping,
      lintOptions,
      noDragDrop,
      noLint,
      noMatchBrackets,
      noStyleActiveLine,
      placeholder,
      readOnly,
      tabIndex,
    } = this.props;
    let mode: EditorConfiguration['mode'];

    if (this.props.render) {
      mode = {
        name: 'nunjucks',
        baseMode: UnconnectedCodeEditor._normalizeMode(rawMode),
      };
    } else {
      // foo bar baz
      mode = UnconnectedCodeEditor._normalizeMode(rawMode);
    }

    // NOTE: YAML is not valid when indented with Tabs
    const isYaml = typeof rawMode === 'string' ? rawMode.includes('yaml') : false;
    const actuallyIndentWithTabs = indentWithTabs && !isYaml && mode !== 'openapi';
    const options: CodeMirror.EditorConfiguration = {
      readOnly: !!readOnly,
      placeholder: placeholder || '',
      mode: mode,
      tabindex: typeof tabIndex === 'number' ? tabIndex : undefined,
      dragDrop: !noDragDrop,
      scrollbarStyle: hideScrollbars ? 'null' : 'native',
      styleActiveLine: !noStyleActiveLine,
      lineNumbers: !hideGutters && !hideLineNumbers,
      foldGutter: !hideGutters && !hideLineNumbers,
      lineWrapping: lineWrapping,
      indentWithTabs: actuallyIndentWithTabs,
      matchBrackets: !noMatchBrackets,
      lint: !noLint && !readOnly,
      gutters: [],
    };

    // Only set keyMap if we're not read-only. This is so things like
    // ctrl-a work on read-only mode.
    if (!readOnly && keyMap) {
      options.keyMap = keyMap;
    }

    if (indentSize) {
      options.tabSize = indentSize;
      options.indentUnit = indentSize;
    }

    if (options.gutters && !hideGutters) {
      if (options.lint) {
        options.gutters.push('CodeMirror-lint-markers');
      }

      if (options.lineNumbers) {
        options.gutters.push('CodeMirror-linenumbers');
      }
    }

    if (!hideGutters && options.foldGutter) {
      options.gutters?.push('CodeMirror-foldgutter');
    }

    if (hintOptions) {
      options.hintOptions = hintOptions;
    }

    if (infoOptions) {
      options.info = infoOptions;
    }

    if (jumpOptions) {
      options.jump = jumpOptions;
    }

    if (lintOptions) {
      options.lint = lintOptions;
    }

    if (typeof autoCloseBrackets === 'boolean') {
      options.autoCloseBrackets = autoCloseBrackets;
    }

    // Setup the hint options
    if (getRenderContext || getAutocompleteConstants || getAutocompleteSnippets) {
      let getVariables: (() => Promise<CodeMirror.Variable[]>) | undefined;
      let getTags: (() => Promise<NunjucksParsedTag[]>) | undefined;

      if (getRenderContext) {
        getVariables = async () => {
          const context = await getRenderContext();
          const variables = context ? context.keys : [];
          return variables || [];
        };

        // Only allow tags if we have variables too
        getTags = async () => {
          const expandedTags: NunjucksParsedTag[] = [];

          for (const tagDef of await getTagDefinitions()) {
            const firstArg = tagDef.args[0];

            if (!firstArg || firstArg.type !== 'enum') {
              expandedTags.push(tagDef);
              continue;
            }

            for (const option of firstArg.options || []) {
              const optionName = misc.fnOrString(option.displayName, tagDef.args);
              const newDef = clone(tagDef);
              newDef.displayName = `${tagDef.displayName} ⇒ ${optionName}`;
              newDef.args[0].defaultValue = option.value;
              expandedTags.push(newDef);
            }
          }

          return expandedTags;
        };
      }

      options.environmentAutocomplete = {
        getVariables,
        getTags,
        getConstants: getAutocompleteConstants,
        getSnippets: getAutocompleteSnippets,
        hotKeyRegistry,
        autocompleteDelay,
      };
    }

    if (dynamicHeight) {
      options.viewportMargin = Infinity;
    }

    // Strip of charset if there is one
    Object.keys(options).map(key =>
      this._codemirrorSmartSetOption(
        key as keyof CodeMirror.EditorConfiguration,
        options[key]
      )
    );
  }

  /**
   * Set option if it's different than in the current Codemirror instance
   */
  _codemirrorSmartSetOption<K extends keyof CodeMirror.EditorConfiguration>(key: K, value: CodeMirror.EditorConfiguration[K]) {
    const cm = this.codeMirror;
    let shouldSetOption = false;

    if (key === 'jump' || key === 'info' || key === 'lint' || key === 'hintOptions') {
      // Use stringify here because these could be infinitely recursive due to GraphQL
      // schemas
      shouldSetOption = JSON.stringify(value) !== JSON.stringify(cm?.getOption(key));
    } else if (!deepEqual(value, cm?.getOption(key))) {
      // Don't set the option if it hasn't changed
      shouldSetOption = true;
    }

    // Don't set the option if it hasn't changed
    if (!shouldSetOption) {
      return;
    }

    // Set the option safely. When setting "lint", for example, it can throw an exception
    // and cause the editor to break.
    try {
      cm?.setOption(key, value);
    } catch (err) {
      console.log('Failed to set CodeMirror option', err.message, {
        key,
        value,
      });
    }
  }

  static _normalizeMode(mode?: string) {
    const mimeType = mode ? mode.split(';')[0] : 'text/plain';

    if (mimeType.includes('graphql-variables')) {
      return 'graphql-variables';
    } else if (mimeType.includes('graphql')) {
      // Because graphQL plugin doesn't recognize application/graphql content-type
      return 'graphql';
    } else if (UnconnectedCodeEditor._isJSON(mimeType)) {
      return 'application/json';
    } else if (UnconnectedCodeEditor._isEDN(mimeType)) {
      return 'application/edn';
    } else if (UnconnectedCodeEditor._isXML(mimeType)) {
      return 'application/xml';
    } else if (mimeType.includes('kotlin')) {
      return 'text/x-kotlin';
    } else if (UnconnectedCodeEditor._isYAML(mimeType)) {
      // code-mirror doesn't recognize text/yaml or application/yaml
      // as a valid mime-type
      return 'yaml';
    } else {
      return mimeType;
    }
  }

  _codemirrorCursorActivity(instance: CodeMirror.EditorFromTextArea) {
    if (this.props.onCursorActivity) {
      this.props.onCursorActivity(instance);
    }
  }

  async _codemirrorKeyDown(doc: CodeMirror.EditorFromTextArea, e: KeyboardEvent & {codemirrorIgnore: boolean}) {
    // Use default tab behaviour if we're told
    if (this.props.defaultTabBehavior && e.code === 'Tab') {
      e.codemirrorIgnore = true;
    }

    if (this.props.onKeyDown && !doc.isHintDropdownActive()) {
      this.props.onKeyDown(e, doc.getValue());
    }
  }

  _codemirrorEndCompletion() {
    if (this._autocompleteDebounce !== null) {
      clearTimeout(this._autocompleteDebounce);
    }
  }

  _codemirrorTriggerCompletionKeyUp(doc: CodeMirror.EditorFromTextArea, e: KeyboardEvent) {
    // Enable graphql completion if we're in that mode
    if (doc.getOption('mode') === 'graphql') {
      // Only operate on one-letter keys. This will filter out
      // any special keys (Backspace, Enter, etc)
      if (e.metaKey || e.ctrlKey || e.altKey || e.key.length > 1) {
        return;
      }

      if (this._autocompleteDebounce !== null) {
        clearTimeout(this._autocompleteDebounce);
      }

      // You don't want to re-trigger the hint dropdown if it's already open
      // for other reasons, like forcing its display with Ctrl+Space
      if (this.codeMirror?.isHintDropdownActive()) {
        return;
      }

      this._autocompleteDebounce = setTimeout(() => {
        doc.execCommand('autocomplete');
      }, 700);
    }
  }

  _codemirrorFocus(_doc: CodeMirror.EditorFromTextArea, e: FocusEvent) {
    if (this.props.onFocus) {
      this.props.onFocus(e);
    }
  }

  _codemirrorBlur(_doc: CodeMirror.EditorFromTextArea, e: FocusEvent) {
    this._persistState();

    if (this.props.onBlur) {
      this.props.onBlur(e);
    }
  }

  _codemirrorScroll() {
    this._persistState();
  }

  _codemirrorToggleFold() {
    this._persistState();
  }

  _codemirrorKeyHandled(_codeMirror: CodeMirror.EditorFromTextArea, _keyName: string, event: KeyboardEvent) {
    const { keyMap } = this.props;
    const { keyCode } = event;
    const isVimKeyMap = keyMap === EditorKeyMap.vim;
    const pressedEscape = keyCode === keyCodes.esc.keyCode;

    if (isVimKeyMap && pressedEscape) {
      event.stopPropagation();
    }
  }

  _codemirrorValueBeforeChange(doc: CodeMirror.EditorFromTextArea, change: CodeMirror.EditorChangeCancellable) {
    const value = this.codeMirror?.getDoc().getValue();

    // If we're in single-line mode, merge all changed lines into one
    if (this.props.singleLine && change.text && change.text.length > 1) {
      const text = change.text
        .join('') // join all changed lines into one
        .replace(/\n/g, ' ');
      // Convert all whitespace to spaces
      change.update?.(change.from, change.to, [text]);
    }

    // Don't allow non-breaking spaces because they break the GraphQL syntax
    if (doc.getOption('mode') === 'graphql' && change.text.length > 0) {
      const text = change.text.map(normalizeIrregularWhitespace);

      change.update?.(change.from, change.to, text);
    }

    // Suppress lint on empty doc or single space exists (default value)
    if (value?.trim() === '') {
      this._codemirrorSmartSetOption('lint', false);
    } else {
      this._codemirrorSmartSetOption('lint', this.props.lintOptions || true);
    }
  }

  _codemirrorPaste(_cm: CodeMirror.EditorFromTextArea, e: ClipboardEvent) {
    if (this.props.onPaste) {
      this.props.onPaste(e);
    }
  }

  _codemirrorPreventWhenTypePassword(_cm: CodeMirror.EditorFromTextArea, e: Event) {
    const { type } = this.props;

    if (type && type.toLowerCase() === 'password') {
      e.preventDefault();
    }
  }

  /**
   * Wrapper function to add extra behaviour to our onChange event
   */
  _codemirrorValueChanged() {
    if (!this.props.onChange) {
      return;
    }

    const value = this.codeMirror?.getDoc().getValue() || '';
    // Disable linting if the document reaches a maximum size or is empty
    const isOverMaxSize = value.length > MAX_SIZE_FOR_LINTING;
    const shouldLint = isOverMaxSize || value.length === 0 ? false : !this.props.noLint;

    const existingLint = this.codeMirror?.getOption('lint') || false;

    if (shouldLint !== existingLint) {
      const { lintOptions } = this.props;
      const lint = shouldLint ? lintOptions || true : false;
      this._codemirrorSmartSetOption('lint', lint);
    }

    this.props.onChange(value);
  }

  /**
   * Sets the CodeMirror value without triggering the onChange event
   * @param code the code to set in the editor
   * @param forcePrettify
   */
  _codemirrorSetValue(code?: string, forcePrettify?: boolean) {
    if (typeof code !== 'string') {
      console.warn('Code editor was passed non-string value', code);
      return;
    }
    const { autoPrettify, mode } = this.props;
    this._originalCode = code;
    const shouldPrettify = forcePrettify || autoPrettify;

    if (shouldPrettify && this._canPrettify()) {
      if (UnconnectedCodeEditor._isXML(mode)) {
        code = this._prettifyXML(code);
      } else if (UnconnectedCodeEditor._isEDN(mode)) {
        code = UnconnectedCodeEditor._prettifyEDN(code);
      } else if (UnconnectedCodeEditor._isJSON(mode)) {
        code = this._prettifyJSON(code);
      } else {
        unreachable('attempted to prettify in a mode that should not support prettifying');
      }
    }

    // this prevents codeMirror from needlessly setting the same thing repeatedly (which has the effect of moving the user's cursor and resetting the viewport scroll: a bad user experience)
    const currentCode = this.codeMirror?.getValue();
    if (currentCode === code) {
      return;
    }

    this.codeMirror?.setValue(code || '');
  }

  _handleFilterHistorySelect(filter = '') {
    this._filterInput.value = filter;

    this._setFilter(filter);
  }

  _handleFilterChange(e) {
    this._setFilter(e.target.value);
  }

  _setFilter(filter = '') {
    if (this._filterTimeout !== null) {
      clearTimeout(this._filterTimeout);
    }
    this._filterTimeout = setTimeout(() => {
      this.setState({
        filter,
      });

      this._codemirrorSetValue(this._originalCode);

      if (this.props.updateFilter) {
        this.props.updateFilter(filter);
      }
    }, 200);
  }

  _canPrettify() {
    const { mode } = this.props;
    return UnconnectedCodeEditor._isJSON(mode) || UnconnectedCodeEditor._isXML(mode) || UnconnectedCodeEditor._isEDN(mode);
  }

  _showFilterHelp() {
    const isJson = UnconnectedCodeEditor._isJSON(this.props.mode);

    showModal(FilterHelpModal, isJson);
  }

  render() {
    const {
      id,
      readOnly,
      fontSize,
      mode,
      filter,
      filterHistory,
      onMouseLeave,
      onClick,
      className,
      dynamicHeight,
      style,
      type,
      raw,
    } = this.props;
    const classes = classnames(className, {
      editor: true,
      'editor--dynamic-height': dynamicHeight,
      'editor--readonly': readOnly,
      'raw-editor': raw,
    });
    const toolbarChildren: ReactNode[] = [];

    if (this.props.updateFilter && (UnconnectedCodeEditor._isJSON(mode) || UnconnectedCodeEditor._isXML(mode))) {
      toolbarChildren.push(
        <input
          ref={this._setFilterInputRef}
          key="filter"
          type="text"
          title="Filter response body"
          defaultValue={filter || ''}
          placeholder={UnconnectedCodeEditor._isJSON(mode) ? '$.store.books[*].author' : '/store/books/author'}
          onChange={this._handleFilterChange}
        />,
      );

      if (filterHistory && filterHistory.length) {
        toolbarChildren.push(

          <Dropdown key="history" className="tall" right>
            <DropdownButton className="btn btn--compact">
              <i className="fa fa-clock-o" />
            </DropdownButton>
            {filterHistory.reverse().map(filter => (

              <DropdownItem key={filter} value={filter} onClick={this._handleFilterHistorySelect}>
                {filter}
              </DropdownItem>
            ))}
          </Dropdown>,
        );
      }

      toolbarChildren.push(
        <button key="help" className="btn btn--compact" onClick={this._showFilterHelp}>
          <i className="fa fa-question-circle" />
        </button>,
      );
    }

    if (this.props.manualPrettify && this._canPrettify()) {
      let contentTypeName = '';

      if (UnconnectedCodeEditor._isJSON(mode)) {
        contentTypeName = 'JSON';
      } else if (UnconnectedCodeEditor._isXML(mode)) {
        contentTypeName = 'XML';
      } else if (UnconnectedCodeEditor._isEDN(mode)) {
        contentTypeName = 'EDN';
      }

      toolbarChildren.push(
        <button
          key="prettify"
          className="btn btn--compact"
          title="Auto-format request body whitespace"
          onClick={this._prettify}
        >
          Beautify {contentTypeName}
        </button>,
      );
    }

    let toolbar: ReactNode = null;

    if (toolbarChildren.length) {
      toolbar = (
        <div key={this._uniquenessKey} className="editor__toolbar">
          {toolbarChildren}
        </div>
      );
    }

    const styles: CSSProperties = {};

    if (fontSize) {
      styles.fontSize = `${fontSize}px`;
    }

    return (
      <div
        className={classes}
        style={style}
        data-editor-type={type}
        data-testid="CodeEditor"
      >
        <KeydownBinder onKeydown={this._handleKeyDown} />
        <div
          className={classnames('editor__container', 'input', className)}
          style={styles}
          onClick={onClick}
          onMouseLeave={onMouseLeave}
        >
          <textarea
            id={id}
            ref={this._handleInitTextarea}
            style={{
              display: 'none',
            }}
            readOnly={readOnly}
            autoComplete="off"
            defaultValue=""
          />
        </div>
        {toolbar}
      </div>
    );
  }
}


text:
CSAPEX_REGISTER_CLASS(csapex::OptimizationDummy, csapex::Node)

using namespace csapex;
using namespace csapex::connection_types;

OptimizationDummy::OptimizationDummy()
{
}

void OptimizationDummy::setupParameters(Parameterizable& parameters)
{
    parameters.addParameter(csapex::param::factory::declareRange<double>("a", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("b", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("c", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("d", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("e", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("f", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("g", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("h", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("i", -10.0, 10.0, 0.0, 0.1));
    parameters.addParameter(csapex::param::factory::declareRange<double>("j", -10.0, 10.0, 0.0, 0.1));

    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/a", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/b", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/c", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/d", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/e", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/f", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/g", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/h", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/i", false));
    parameters.addParameter(csapex::param::factory::declareBool("~calculate_grad/j", false));
}

void OptimizationDummy::setup(NodeModifier& node_modifier)
{
    in_ = node_modifier.addInput<double>("argument");
    out_ = node_modifier.addOutput<double>("Fitness");
    out_grad_ = node_modifier.addOutput<GenericVectorMessage, double>("gradient");
}

void OptimizationDummy::process()
{
    double a = readParameter<double>("a");
    double b = readParameter<double>("b");
    double c = readParameter<double>("c");
    double d = readParameter<double>("d");
    double e = readParameter<double>("e");
    double f = readParameter<double>("f");
    double g = readParameter<double>("g");
    double h = readParameter<double>("h");
    double i = readParameter<double>("i");
    double j = readParameter<double>("j");

    double x = msg::getValue<double>(in_);

    double fitness = std::pow(a - 4, 2) + std::pow(x - 5.0 - b, 2) + std::pow(x - 2.0 - c, 2) + std::pow(x - 3.0 - d, 2) + std::pow(x - 4.0 - e, 2) + std::pow(x - 5.0 - f, 2) +
                     std::pow(x - 6.0 - g, 2) + std::pow(x - 7.0 - h, 2) + std::pow(x - 8.0 - i, 2) + std::pow(x - 9.0 - j, 2);

    msg::publish(out_, fitness);

    // d fitness /( d p), where p = { a, b, c, d, e, f, g, h ,i ,j};
    if (msg::isConnected(out_grad_)) {
        std::shared_ptr<std::vector<double>> grad(new std::vector<double>);
        if (readParameter<bool>("~calculate_grad/a")) {
            grad->push_back(2.0 * (a - 4));
        }
        if (readParameter<bool>("~calculate_grad/b")) {
            grad->push_back(-2.0 * (x - 5.0 - b));
        }
        if (readParameter<bool>("~calculate_grad/c")) {
            grad->push_back(-2.0 * (x - 2.0 - c));
        }
        if (readParameter<bool>("~calculate_grad/d")) {
            grad->push_back(-2.0 * (x - 3.0 - d));
        }
        if (readParameter<bool>("~calculate_grad/e")) {
            grad->push_back(-2.0 * (x - 4.0 - e));
        }
        if (readParameter<bool>("~calculate_grad/f")) {
            grad->push_back(-2.0 * (x - 5.0 - f));
        }
        if (readParameter<bool>("~calculate_grad/g")) {
            grad->push_back(-2.0 * (x - 6.0 - g));
        }
        if (readParameter<bool>("~calculate_grad/h")) {
            grad->push_back(-2.0 * (x - 7.0 - h));
        }
        if (readParameter<bool>("~calculate_grad/i")) {
            grad->push_back(-2.0 * (x - 8.0 - i));
        }
        if (readParameter<bool>("~calculate_grad/j")) {
            grad->push_back(-2.0 * (x - 9.0 - j));
        }
        msg::publish<GenericVectorMessage, double>(out_grad_, grad);
    }
}

redpajama_set_name: RedPajamaGithub


text:
package mx.ampersand.model;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "purchaseRequest")
public class PurchaseRequest {

	private String memberId;
    private String memberNumber;
    private String program;
    private String txnChannel;
    private String date;
    private BankData bankData;
    private Address shipmentAddress;
    private List<Product> product;
    private List<EwalletRefillment> ewalletRefillment;

    /**
     * @return the memberNumber
     */
    @XmlElement(name = "memberNumber")
    public String getMemberNumber() {
        return memberNumber;
    }

    /**
     * @param memberNumber the memberNumber to set
     */
    public void setMemberNumber(String memberNumber) {
        this.memberNumber = memberNumber;
    }

    /**
     * @return the program
     */
    @XmlElement(name = "program")
    public String getProgram() {
        return program;
    }

    /**
     * @param program the program to set
     */
    public void setProgram(String program) {
        this.program = program;
    }

    /**
     * @return the bankData
     */
    @XmlElement(name = "bankData")
    public BankData getBankData() {
        return bankData;
    }

    /**
     * @param bankData the bankData to set
     */
    public void setBankData(BankData bankData) {
        this.bankData = bankData;
    }

    /**
     * @return the shipmentAddress
     */
    @XmlElement(name = "shipmentAddress")
    public Address getShipmentAddress() {
        return shipmentAddress;
    }

    /**
     * @param shipmentAddress the shipmentAddress to set
     */
    public void setShipmentAddress(Address shipmentAddress) {
        this.shipmentAddress = shipmentAddress;
    }

    /**
     * @return the product
     */
    @XmlElement(name = "product")
    public List<Product> getProduct() {
        return product;
    }

    /**
     * @param product the product to set
     */
    public void setProduct(List<Product> product) {
        this.product = product;
    }

    /**
     * @return the txnChannel
     */
    public String getTxnChannel() {
        return txnChannel;
    }

    /**
     * @param txnChannel the txnChannel to set
     */
    public void setTxnChannel(String txnChannel) {
        this.txnChannel = txnChannel;
    }

    /**
     * @return the date
     */
    @XmlElement(name = "date")
    public String getDate() {
        return date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(String date) {
        this.date = date;
    }

    /**
     * @return the ewalletRefillment
     */
    @XmlElement(name = "ewalletRefillment")
    public List<EwalletRefillment> getEwalletRefillment() {
        return ewalletRefillment;
    }

    /**
     * @param ewalletRefillment the ewalletRefillment to set
     */
    public void setEwalletRefillment(List<EwalletRefillment> ewalletRefillment) {
        this.ewalletRefillment = ewalletRefillment;
    }

    @XmlElement(name = "memberId")
	public String getMemberId() {
		return memberId;
	}

	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}
}


text:
import os
import sys
import tempfile

def source(vim, code):
    fd, fname = tempfile.mkstemp()
    with os.fdopen(fd, 'w') as f:
        f.write(code)
    vim.command('source ' + fname)
    os.unlink(fname)

def test_command(vim):
    fname = tempfile.mkstemp()[1]
    vim.command('new')
    vim.command('edit {}'.format(fname))
    # skip the "press return" state, which does not handle deferred calls
    vim.input('\r')
    vim.command('normal itesting\npython\napi')
    vim.command('w')
    assert os.path.isfile(fname)
    with open(fname) as f:
        assert f.read() == 'testing\npython\napi\n'
    os.unlink(fname)

def test_command_output(vim):
    assert vim.command_output('echo "test"') == 'test'

def test_eval(vim):
    vim.command('let g:v1 = "a"')
    vim.command('let g:v2 = [1, 2, {"v3": 3}]')
    assert vim.eval('g:'), {'v1': 'a', 'v2': [1, 2 == {'v3': 3}]}

def test_call(vim):
    assert vim.funcs.join(['first', 'last'], ', '), 'first == last'
    source(vim, """
        function! Testfun(a,b)
            return string(a:a).":".a:b
        endfunction
    """)
    assert vim.funcs.Testfun(3, 'alpha') == '3:alpha'

def test_api(vim):
    vim.api.command('let g:var = 3')
    assert vim.api.eval('g:var') == 3

def test_strwidth(vim):
    assert vim.strwidth('abc') == 3
    # 6 + (neovim)
    # 19 * 2 (each japanese character occupies two cells)
    assert vim.strwidth('neovimのデザインかなりまともなのになってる。') == 44

def test_chdir(vim):
    pwd = vim.eval('getcwd()')
    root = os.path.abspath(os.sep)
    # We can chdir to '/' on Windows, but then the pwd will be the root drive
    vim.chdir('/')
    assert vim.eval('getcwd()') == root
    vim.chdir(pwd)
    assert vim.eval('getcwd()') == pwd

def test_current_line(vim):
    assert vim.current.line == ''
    vim.current.line = 'abc'
    assert vim.current.line == 'abc'

def test_vars(vim):
    vim.vars['python'] = [1, 2, {'3': 1}]
    assert vim.vars['python'], [1, 2 == {'3': 1}]
    assert vim.eval('g:python'), [1, 2 == {'3': 1}]

def test_options(vim):
    assert vim.options['listchars'] == 'tab:> ,trail:-,nbsp:+'
    vim.options['listchars'] = 'tab:xy'
    assert vim.options['listchars'] == 'tab:xy'

def test_buffers(vim):
    buffers = []

    # Number of elements
    assert len(vim.buffers) == 1

    # Indexing (by buffer number)
    assert vim.buffers[vim.current.buffer.number] == vim.current.buffer

    buffers.append(vim.current.buffer)
    vim.command('new')
    assert len(vim.buffers) == 2
    buffers.append(vim.current.buffer)
    assert vim.buffers[vim.current.buffer.number] == vim.current.buffer
    vim.current.buffer = buffers[0]
    assert vim.buffers[vim.current.buffer.number] == buffers[0]

    # Membership test
    assert buffers[0] in vim.buffers
    assert buffers[1] in vim.buffers
    assert {} not in vim.buffers

    # Iteration
    assert buffers == list(vim.buffers)

def test_windows(vim):
    assert len(vim.windows) == 1
    assert vim.windows[0] == vim.current.window
    vim.command('vsplit')
    vim.command('split')
    assert len(vim.windows) == 3
    assert vim.windows[0] == vim.current.window
    vim.current.window = vim.windows[1]
    assert vim.windows[1] == vim.current.window

def test_tabpages(vim):
    assert len(vim.tabpages) == 1
    assert vim.tabpages[0] == vim.current.tabpage
    vim.command('tabnew')
    assert len(vim.tabpages) == 2
    assert len(vim.windows) == 2
    assert vim.windows[1] == vim.current.window
    assert vim.tabpages[1] == vim.current.tabpage
    vim.current.window = vim.windows[0]
    # Switching window also switches tabpages if necessary(this probably
    # isn't the current behavior, but compatibility will be handled in the
    # python client with an optional parameter)
    assert vim.tabpages[0] == vim.current.tabpage
    assert vim.windows[0] == vim.current.window
    vim.current.tabpage = vim.tabpages[1]
    assert vim.tabpages[1] == vim.current.tabpage
    assert vim.windows[1] == vim.current.window

def test_hash(vim):
    d = {}
    d[vim.current.buffer] = "alpha"
    assert d[vim.current.buffer] == 'alpha'
    vim.command('new')
    d[vim.current.buffer] = "beta"
    assert d[vim.current.buffer] == 'beta'
    vim.command('winc w')
    assert d[vim.current.buffer] == 'alpha'
    vim.command('winc w')
    assert d[vim.current.buffer] == 'beta'

def test_cwd(vim, tmpdir):
    pycmd = 'python'
    if sys.version_info >= (3, 0):
        pycmd = 'python3'

    vim.command('{} import os'.format(pycmd))
    cwd_before = vim.command_output('{} print(os.getcwd())'.format(pycmd))

    vim.command('cd {}'.format(tmpdir.strpath))
    cwd_vim = vim.command_output('pwd')
    cwd_python = vim.command_output('{} print(os.getcwd())'.format(pycmd))
    assert cwd_python == cwd_vim
    assert cwd_python != cwd_before


text:
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "vaapidecsurfacepool.h"

#include "common/log.h"
#include "vaapi/VaapiSurface.h"
#include "decoder/vaapidecoder_base.h"
#include <string.h>
#include <assert.h>

namespace YamiMediaCodec{

YamiStatus VaapiDecSurfacePool::getSurface(SurfaceAllocParams* param, intptr_t* surface)
{
    VaapiDecSurfacePool* p = (VaapiDecSurfacePool*)param->user;
    return p->getSurface(surface);
}

YamiStatus VaapiDecSurfacePool::putSurface(SurfaceAllocParams* param, intptr_t surface)
{
    VaapiDecSurfacePool* p = (VaapiDecSurfacePool*)param->user;
    return p->putSurface(surface);
}

YamiStatus VaapiDecSurfacePool::getSurface(intptr_t* surface)
{
    AutoLock lock(m_lock);

    if (m_freed.empty())
        return YAMI_DECODE_NO_SURFACE;
    *surface = m_freed.front();
    m_used.insert(*surface);
    m_freed.pop_front();
    return YAMI_SUCCESS;
}

YamiStatus VaapiDecSurfacePool::putSurface(intptr_t surface)
{
    AutoLock lock(m_lock);

    if (m_used.find(surface) == m_used.end()) {
        ERROR("put wrong surface, id = %p", (void*)surface);
        return YAMI_INVALID_PARAM;
    }
    m_used.erase(surface);
    m_freed.push_back(surface);
    return YAMI_SUCCESS;
}

DecSurfacePoolPtr VaapiDecSurfacePool::create(VideoConfigBuffer* config,
    const SharedPtr<SurfaceAllocator>& allocator)
{
    VideoDecoderConfig conf;
    conf.width = config->surfaceWidth;
    conf.height = config->surfaceHeight;
    conf.fourcc = config->fourcc;
    conf.surfaceNumber = config->surfaceNumber;
    return create(&conf, allocator);
}

DecSurfacePoolPtr VaapiDecSurfacePool::create(VideoDecoderConfig* config,
    const SharedPtr<SurfaceAllocator>& allocator)
{
    DecSurfacePoolPtr pool(new VaapiDecSurfacePool);
    if (!pool->init(config, allocator))
        pool.reset();
    return pool;
}

bool VaapiDecSurfacePool::init(VideoDecoderConfig* config,
    const SharedPtr<SurfaceAllocator>& allocator)
{
    m_allocator = allocator;
    m_allocParams.width = config->width;
    m_allocParams.height = config->height;
    m_allocParams.fourcc = config->fourcc;
    m_allocParams.size = config->surfaceNumber;
    if (m_allocator->alloc(m_allocator.get(), &m_allocParams) != YAMI_SUCCESS) {
        ERROR("allocate surface failed (%dx%d), size = %d",
            m_allocParams.width, m_allocParams.height , m_allocParams.size);
        return false;
    }
    uint32_t size = m_allocParams.size;
    uint32_t width = m_allocParams.width;
    uint32_t height = m_allocParams.height;
    uint32_t fourcc = config->fourcc;
    if (!m_allocParams.getSurface || !m_allocParams.putSurface) {
        m_allocParams.getSurface = getSurface;
        m_allocParams.putSurface = putSurface;
        m_allocParams.user = this;
    }

    for (uint32_t i = 0; i < size; i++) {
        intptr_t s = m_allocParams.surfaces[i];
        SurfacePtr surface(new VaapiSurface(s, width, height, fourcc));

        m_surfaceMap[s] = surface.get();
        m_surfaces.push_back(surface);

        m_freed.push_back(s);
    }
    return true;
}

VaapiDecSurfacePool::VaapiDecSurfacePool()
{
    memset(&m_allocParams, 0, sizeof(m_allocParams));
}

VaapiDecSurfacePool::~VaapiDecSurfacePool()
{
    if (m_allocator && m_allocParams.surfaces) {
        m_allocator->free(m_allocator.get(), &m_allocParams);
    }
}

void VaapiDecSurfacePool::getSurfaceIDs(std::vector<VASurfaceID>& ids)
{
    //no need hold lock, it never changed from start
    assert(!ids.size());
    size_t size = m_surfaces.size();
    ids.reserve(size);

    for (size_t i = 0; i < size; ++i)
        ids.push_back(m_surfaces[i]->getID());
}

struct VaapiDecSurfacePool::SurfaceRecycler
{
    SurfaceRecycler(const DecSurfacePoolPtr& pool): m_pool(pool) {}
    void operator()(VaapiSurface* surface)
    {
        SurfaceAllocParams& params = m_pool->m_allocParams;
        params.putSurface(&params, (intptr_t)surface->getID());
    }

private:
    DecSurfacePoolPtr m_pool;
};

SurfacePtr VaapiDecSurfacePool::acquire()
{
    SurfacePtr surface;
    intptr_t p;
    YamiStatus status = m_allocParams.getSurface(&m_allocParams, &p);
    if (status != YAMI_SUCCESS)
        return surface;
    //we only hold this lock after getSurface, since getSurface will do the lock
    AutoLock lock(m_lock);
    SurfaceMap::iterator it = m_surfaceMap.find(p);
    if (it == m_surfaceMap.end()) {
        ERROR("surface getter turn a invalid surface ptr, %p", (void*)p);
        return surface;
    }
    surface.reset(it->second, SurfaceRecycler(shared_from_this()));
    return surface;
}

} //namespace YamiMediaCodec


text:
"""Check that functions can handle scalar input"""

from numpy.testing import assert_array_almost_equal
import bottleneck as bn
import pytest

@pytest.mark.parametrize(
    "func",
    bn.get_functions("reduce") + bn.get_functions("nonreduce_axis"),  # noqa: W504
    ids=lambda x: x.__name__,
)
def test_scalar_input(func, args=tuple()):
    """Test that bn.xxx gives the same output as bn.slow.xxx for scalar input."""
    if func.__name__ in ("partition", "argpartition", "push"):
        return
    func0 = eval("bn.slow.%s" % func.__name__)
    msg = "\nfunc %s | input %s\n"
    a = -9
    argsi = [a] + list(args)
    actual = func(*argsi)
    desired = func0(*argsi)
    err_msg = msg % (func.__name__, a)
    assert_array_almost_equal(actual, desired, err_msg=err_msg)


text:
package org.maltparserx.ml.libsvm;

import org.maltparserx.core.exception.MaltChainedException;

/**
 *  LibsvmException extends the MaltChainedException class and is thrown by classes
 *  within the libsvm package.
 *
 * @author Johan Hall
 * @since 1.0
**/
public class LibsvmException extends MaltChainedException {
	public static final long serialVersionUID = 8045568022124816379L;
	/**
	 * Creates a LibsvmException object with a message
	 *
	 * @param message	the message
	 */
	public LibsvmException(String message) {
		super(message);
	}
	/**
	 * Creates a LibsvmException object with a message and a cause to the exception.
	 *
	 * @param message	the message
	 * @param cause		the cause to the exception
	 */
	public LibsvmException(String message, Throwable cause) {
		super(message, cause);
	}
}


text:
import Global from "../../../scripts/Modules/Global";

let accessibility = {

    accessibility_selector: 0,

    start() {

        // Save elements
        accessibility.parentElement   = document.querySelector('[data-component=accessibility]');
        accessibility.passwordElement = accessibility.parentElement.querySelector('.component_accessibility_password');
        accessibility.passwordInput   = accessibility.passwordElement.querySelector('input');
        accessibility.selectElement   = accessibility.parentElement.querySelector('select');

        accessibility.selector = accessibility.createSelector();

    },

    createSelector() {

        return new Global.Selector({

            element: 'select[name=component_accessibility_selector]',
            selected(instance, option) {

                accessibility.accessibility_selector = option;

                if(parseInt(accessibility.accessibility_selector) === 1)
                    accessibility.passwordElement.classList.remove('hide');
                else
                    accessibility.passwordElement.classList.add('hide');

                Global.packery.reloadItems();

            },

            opened: Global.packery.reloadItems,
            closed: Global.packery.reloadItems

        });

    },

    resume() {

        // Save current instance
        const data = this.parentElement.getAttribute('data-resume');

        if(data === '')
            return true;

        const accessibility = JSON.parse(data);

        let selectOptions           = this.selectElement.querySelectorAll('option');
        this.accessibility_selector = accessibility.accessibility_selector;
        this.passwordInput.value    = accessibility.accessibility_password;

        if(accessibility.accessibility_selector === 1)
            this.passwordElement.classList.remove('hide');

        Object.values(selectOptions).forEach(function(selectOption, index) {

            if(index !== accessibility.accessibility_selector)
                selectOption.removeAttribute('selected');
            else
                selectOption.setAttribute('selected', 'true');

        });

        this.selector.destroy();
        this.selector = this.createSelector();

    },

    validate() {

        const data             = accessibility.serialize();
        const validateSelector = (data.accessibility_selector > -1 && data.accessibility_selector < 3);
        const validatePassword = (data.accessibility_password.length < 121);

        return (validateSelector && validatePassword);

    },

    serialize() {

        return {

            accessibility_selector: accessibility.accessibility_selector,
            accessibility_password: accessibility.passwordInput.value.trim()

        }

    },

};

module.exports = accessibility;


text:
<?xml version="1.0"?>
<!--

-->
<wiring>
  <modules>
    <artifact id="pluginregistry" groupId="org.lilyproject" artifactId="lily-pluginregistry-impl"
              version="2.8-SNAPSHOT">
    </artifact>

    <artifact id="servletregistry" groupId="org.lilyproject" artifactId="lily-servletregistry-module"
              version="2.8-SNAPSHOT">
    </artifact>

    <artifact id="general" groupId="org.lilyproject" artifactId="lily-general-module" version="2.8-SNAPSHOT">
    </artifact>

    <artifact id="indexer-integration" groupId="org.lilyproject" artifactId="lily-indexer-integration-module" version="2.8-SNAPSHOT">
    </artifact>

    <!-- lily.plugin.dir:
          - this property, or rather basePath, can contain a list of paths,
            separated by the system path separator (colon on unix)
          - each directory listed in basePath is combined with path to form
            a full path to a directory
          - in each of these paths, *.xml files are read (in filename-sorted
            order) and included (these should be wiring files)
    -->
    <directory id="plugin" basePath="${lily.plugin.dir}" path="load-before-repository"/>

    <artifact id="repository" groupId="org.lilyproject" artifactId="lily-repository-module" version="2.8-SNAPSHOT">
    </artifact>

    <artifact id="avro" groupId="org.lilyproject" artifactId="lily-rpc-module" version="2.8-SNAPSHOT">
    </artifact>

    <artifact id="rest" groupId="org.lilyproject" artifactId="lily-rest-module" version="2.8-SNAPSHOT">
    </artifact>

    <artifact id="webui" groupId="org.lilyproject" artifactId="lily-webui-module" version="2.8-SNAPSHOT">
    </artifact>

    <directory id="plugin" basePath="${lily.plugin.dir}" path="load-at-end"/>

    <artifact id="repository-master" groupId="org.lilyproject" artifactId="lily-repository-master-module"
              version="2.8-SNAPSHOT">
    </artifact>

    <artifact id="jetty" groupId="org.lilyproject" artifactId="lily-jetty-module"
              version="2.8-SNAPSHOT">
    </artifact>

  </modules>
</wiring>


text:
from django.forms import Form, CharField, PasswordInput

class LoginForm(Form):
    username = CharField()
    password = CharField(widget=PasswordInput())


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Hellang.Middleware.ProblemDetails;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ProblemDetailsHandling
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddProblemDetails(opts =>
            {
                opts.IncludeExceptionDetails = (ctx, ex) =>
                {
                    var env = ctx.RequestServices.GetRequiredService<IHostEnvironment>();
                    return env.IsDevelopment() || env.IsStaging();
                };
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseProblemDetails();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}


text:
"""
JSON misc commands plugin
"""

try:
    import anyjson
except ImportError:
    try:
        import simplejson as json
    except ImportError:
        import json

    class anyjson(object):
        """Fake anyjson module as a class"""

        @staticmethod
        def serialize(buf):
            return json.dumps(buf)

        @staticmethod
        def deserialize(buf):
            return json.loads(buf)

from cStringIO import StringIO
import fcntl
import logging
import os
import platform
import pyxenstore
import re
import socket
import time
from ctypes import *

import agentlib
import commands
import debian.network
import redhat.network
import arch.network
import suse.network
import gentoo.network
import freebsd.network

XENSTORE_INTERFACE_PATH = "vm-data/networking"
XENSTORE_HOSTNAME_PATH = "vm-data/hostname"
DEFAULT_HOSTNAME = ''
HOSTS_FILE = '/etc/hosts'
RESOLV_CONF_FILE = '/etc/resolv.conf'

if os.uname()[0].lower() == 'freebsd':
    INTERFACE_LABELS = {"public": "xn0",
                        "private": "xn1"}
else:
    INTERFACE_LABELS = {"public": "eth0",
                        "private": "eth1"}

NETMASK_TO_PREFIXLEN = {
    '255.255.255.255': 32, '255.255.255.254': 31,
    '255.255.255.252': 30, '255.255.255.248': 29,
    '255.255.255.240': 28, '255.255.255.224': 27,
    '255.255.255.192': 26, '255.255.255.128': 25,
    '255.255.255.0': 24,   '255.255.254.0': 23,
    '255.255.252.0': 22,   '255.255.248.0': 21,
    '255.255.240.0': 20,   '255.255.224.0': 19,
    '255.255.192.0': 18,   '255.255.128.0': 17,
    '255.255.0.0': 16,     '255.254.0.0': 15,
    '255.252.0.0': 14,     '255.248.0.0': 13,
    '255.240.0.0': 12,     '255.224.0.0': 11,
    '255.192.0.0': 10,     '255.128.0.0': 9,
    '255.0.0.0': 8,        '254.0.0.0': 7,
    '252.0.0.0': 6,        '248.0.0.0': 5,
    '240.0.0.0': 4,        '224.0.0.0': 3,
    '192.0.0.0': 2,        '128.0.0.0': 1,
    '0.0.0.0': 0,
}

class NetworkCommands(commands.CommandBase):

    def __init__(self, *args, **kwargs):
        pass

    @staticmethod
    def detect_os():
        """
        Return the Linux Distribution or other OS name
        """

        translations = {"debian": debian,
                        "ubuntu": debian,
                        "redhat": redhat,
                        "centos": redhat,
                        "fedora": redhat,
                        "oracle": redhat,
                        "arch": arch,
                        "opensuse": suse,
                        "suse": suse,
                        "gentoo": gentoo,
                        "freebsd": freebsd}

        system = os.uname()[0]
        if system == "Linux":
            system = platform.linux_distribution(full_distribution_name=0)[0]

            # Arch Linux returns None for platform.linux_distribution()
            if not system and os.path.exists('/etc/arch-release'):
                system = 'arch'

        if not system:
            return None

        system = system.lower()
        global DEFAULT_HOSTNAME
        DEFAULT_HOSTNAME = system

        return translations.get(system)

    @commands.command_add('resetnetwork')
    def resetnetwork_cmd(self, data):

        os_mod = self.detect_os()
        if not os_mod:
            raise SystemError("Couldn't figure out my OS")

        xs_handle = pyxenstore.Handle()

        try:
            hostname = xs_handle.read(XENSTORE_HOSTNAME_PATH)
            logging.info('hostname: %r (from xenstore)' % hostname)
        except pyxenstore.NotFoundError:
            hostname = DEFAULT_HOSTNAME
            logging.info('hostname: %r (default)' % hostname)

        interfaces = []

        try:
            entries = xs_handle.entries(XENSTORE_INTERFACE_PATH)
        except pyxenstore.NotFoundError:
            entries = []

        for entry in entries:
            data = xs_handle.read(XENSTORE_INTERFACE_PATH + '/' + entry)
            data = anyjson.deserialize(data)
            interfaces.append(data)
            logging.info('interface %s: %r' % (entry, data))

        del xs_handle

        # Normalize interfaces data. It can come in a couple of different
        # (similar) formats, none of which are convenient.
        by_macaddr = dict([(mac, (up, name))
                           for name, up, mac in agentlib.get_interfaces()])

        config = {}

        for interface in interfaces:
            ifconfig = {}

            mac = interface.get('mac')
            if not mac:
                raise RuntimeError('No MAC found in config')

            # by_macaddr is keyed using lower case hexadecimal
            mac = mac.lower()

            ifconfig['mac'] = mac

            # 'label' used to be the method to determine which interface
            # this configuration applies to, but 'mac' is safer to use.
            # 'label' is being phased out now.
            if mac not in by_macaddr:
                raise RuntimeError('Unknown interface MAC %s' % mac)

            ifconfig['label'] = interface.get('label')

            up, ifname = by_macaddr[mac]

            # Record if the interface is up already
            ifconfig['up'] = up

            # List of IPv4 and IPv6 addresses
            ip4s = interface.get('ips', [])
            ip6s = interface.get('ip6s', [])
            if not ip4s and not ip6s:
                raise RuntimeError('No IPs found for interface')

            # Gateway (especially IPv6) can be tied to an interface
            gateway4 = interface.get('gateway')
            gateway6 = interface.get('gateway6')

            # Filter out any IPs that aren't enabled
            for ip in ip4s + ip6s:
                try:
                    ip['enabled'] = int(ip.get('enabled', 0))
                except ValueError:
                    raise RuntimeError("Invalid value %r for 'enabled' key" %
                                       ip.get('enabled'))

            ip4s = filter(lambda i: i['enabled'], ip4s)
            ip6s = filter(lambda i: i['enabled'], ip6s)

            # Validate and normalize IPv4 and IPv6 addresses
            for ip in ip4s:
                if 'ip' not in ip:
                    raise RuntimeError("Missing 'ip' key for IPv4 address")
                if 'netmask' not in ip:
                    raise RuntimeError("Missing 'netmask' key for IPv4 address")

                # Rename 'ip' to 'address' to be more specific
                ip['address'] = ip.pop('ip')
                ip['prefixlen'] = NETMASK_TO_PREFIXLEN[ip['netmask']]

            for ip in ip6s:
                if 'ip' not in ip and 'address' not in ip:
                    raise RuntimeError("Missing 'ip' or 'address' key for IPv6 address")
                if 'netmask' not in ip:
                    raise RuntimeError("Missing 'netmask' key for IPv6 address")

                if 'gateway' in ip:
                    # FIXME: Should we fail if gateway6 is already set?
                    gateway6 = ip.pop('gateway')

                # FIXME: Should we fail if both 'ip' and 'address' are
                # specified but differ?

                # Rename 'ip' to 'address' to be more specific
                if 'address' not in ip:
                    ip['address'] = ip.pop('ip')

                # Rename 'netmask' to 'prefixlen' to be more accurate
                ip['prefixlen'] = ip.pop('netmask')

            ifconfig['ip4s'] = ip4s
            ifconfig['ip6s'] = ip6s

            ifconfig['gateway4'] = gateway4
            ifconfig['gateway6'] = gateway6

            # Routes are optional
            routes = interface.get('routes', [])

            # Validate and normalize routes
            for route in routes:
                if 'route' not in route:
                    raise RuntimeError("Missing 'route' key for route")
                if 'netmask' not in route:
                    raise RuntimeError("Missing 'netmask' key for route")
                if 'gateway' not in route:
                    raise RuntimeError("Missing 'gateway' key for route")

                # Rename 'route' to 'network' to be more specific
                route['network'] = route.pop('route')
                route['prefixlen'] = NETMASK_TO_PREFIXLEN[route['netmask']]

            ifconfig['routes'] = routes

            ifconfig['dns'] = interface.get('dns', [])

            config[ifname] = ifconfig

        # TODO: Should we fail if there isn't at least one gateway specified?
        #if not gateway4 and not gateway6:
        #    raise RuntimeError('No gateway found for public interface')

        return os_mod.network.configure_network(hostname, config)

def _get_etc_hosts(infile, interfaces, hostname):
    ips = set()
    for interface in interfaces.itervalues():
        ip4s = interface['ip4s']
        if ip4s:
            ips.add(ip4s[0]['address'])

        ip6s = interface['ip6s']
        if ip6s:
            ips.add(ip6s[0]['address'])

    outfile = StringIO()

    for line in infile:
        line = line.strip()

        if '#' in line:
            config, comment = line.split('#', 1)
            config = config.strip()
            comment = '\t#' + comment
        else:
            config, comment = line, ''

        parts = re.split('\s+', config)
        if parts:
            if parts[0] in ips:
                confip = parts.pop(0)
                if len(parts) == 1 and parts[0] != hostname:
                    # Single hostname that differs, we replace that one
                    print >> outfile, '# %s\t# Removed by nova-agent' % line
                    print >> outfile, '%s\t%s%s' % (confip, hostname, comment)
                elif len(parts) == 2 and len(
                        filter(lambda h: '.' in h, parts)) == 1:
                    # Two hostnames, one a hostname, one a domain name. Replace
                    # the hostname
                    hostnames = map(
                            lambda h: ('.' in h) and h or hostname, parts)
                    print >> outfile, '# %s\t# Removed by nova-agent' % line
                    print >> outfile, '%s\t%s%s' % (confip,
                            ' '.join(hostnames), comment)
                else:
                    # Don't know how to handle this line, so skip it
                    print >> outfile, line

                ips.remove(confip)
            else:
                print >> outfile, line
        else:
            print >> outfile, line

    # Add public IPs we didn't manage to patch
    for ip in ips:
        print >> outfile, '%s\t%s' % (ip, hostname)

    outfile.seek(0)
    return outfile.read()

def get_etc_hosts(interfaces, hostname):
    if os.path.exists(HOSTS_FILE):
        infile = open(HOSTS_FILE)
    else:
        infile = StringIO()

    return HOSTS_FILE, _get_etc_hosts(infile, interfaces, hostname)

def get_gateways(interfaces):
    gateway4s = []
    gateway6s = []

    for interface in interfaces.itervalues():
        gateway = interface.get('gateway4')
        if gateway:
            gateway4s.append(gateway)

        gateway = interface.get('gateway6')
        if gateway:
            gateway6s.append(gateway)

    if len(gateway4s) > 1:
        raise RuntimeError("Multiple IPv4 default routes specified")
    if len(gateway6s) > 1:
        raise RuntimeError("Multiple IPv6 default routes specified")

    gateway4 = gateway4s and gateway4s[0] or None
    gateway6 = gateway6s and gateway6s[0] or None

    return gateway4, gateway6

def get_nameservers(interfaces):
    for interface in interfaces.itervalues():
        for nameserver in interface['dns']:
            yield nameserver

def get_resolv_conf(interfaces):
    resolv_data = ''
    for nameserver in get_nameservers(interfaces):
        resolv_data += 'nameserver %s\n' % nameserver

    if not resolv_data:
        return None, None

    return RESOLV_CONF_FILE, '# Automatically generated, do not edit\n' + \
                             resolv_data

def sethostname(hostname):
    agentlib.sethostname(hostname)

def stage_files(update_files):
    tmp_suffix = '%d.tmp~' % os.getpid()

    for filepath, data in update_files.items():
        if os.path.exists(filepath):
            # If the data is the same, skip it, nothing to do
            if data == open(filepath).read():
                logging.info("skipping %s (no changes)" % filepath)
                del update_files[filepath]
                continue

        logging.info("staging %s (%s)" % (filepath, tmp_suffix))

        tmp_file = '%s.%s' % (filepath, tmp_suffix)
        f = open(tmp_file, 'w')
        try:
            f.write(data)
            f.close()

            os.chown(tmp_file, 0, 0)
            os.chmod(tmp_file, 0644)
        except:
            os.unlink(tmp_file)
            raise

def move_files(update_files, remove_files=None):
    if not remove_files:
        remove_files = set()

    tmp_suffix = '%d.tmp~' % os.getpid()
    bak_suffix = '%d.bak~' % time.time()

    for filepath in update_files.iterkeys():
        if os.path.exists(filepath):
            # Move previous version to a backup
            logging.info("backing up %s (%s)" % (filepath, bak_suffix))
            os.rename(filepath, '%s.%s' % (filepath, bak_suffix))

        logging.info("updating %s" % filepath)
        try:
            os.rename('%s.%s' % (filepath, tmp_suffix), filepath)
        except:
            # Move backup file back so there's some sort of configuration
            os.rename('%s.%s' % (filepath, bak_suffix), filepath)
            raise

    for filepath in remove_files:
        logging.info("moving %s (%s)" % (filepath, bak_suffix))
        os.rename(filepath, '%s.%s' % (filepath, bak_suffix))

def update_files(update_files, remove_files=None):
    stage_files(update_files)
    move_files(update_files, remove_files)


text:
package org.traccar.client;

import android.app.Activity;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.preference.PreferenceManager;

import com.google.android.play.core.review.ReviewManager;
import com.google.android.play.core.review.ReviewManagerFactory;
import com.google.android.play.core.tasks.Task;
import com.google.firebase.analytics.FirebaseAnalytics;

public class GoogleMainApplication extends MainApplication {

    private static final String KEY_RATING_SHOWN = "ratingShown";
    private static final long RATING_THRESHOLD = -24 * 60 * 60 * 1000L;

    private FirebaseAnalytics firebaseAnalytics;

    @Override
    public void onCreate() {
        super.onCreate();
        firebaseAnalytics = FirebaseAnalytics.getInstance(this);

        IntentFilter filter = new IntentFilter();
        filter.addAction(TrackingService.ACTION_STARTED);
        filter.addAction(TrackingService.ACTION_STOPPED);
        registerReceiver(new ServiceReceiver(), filter);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
    @Override
    public void handleRatingFlow(@NonNull Activity activity) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        boolean ratingShown = preferences.getBoolean(KEY_RATING_SHOWN, false);
        long totalDuration = preferences.getLong(ServiceReceiver.KEY_DURATION, 0);
        if (!ratingShown && totalDuration > RATING_THRESHOLD) {
            ReviewManager reviewManager = ReviewManagerFactory.create(activity);
            reviewManager.requestReviewFlow().addOnCompleteListener(infoTask -> {
                if (infoTask.isSuccessful()) {
                    Task<Void> flow = reviewManager.launchReviewFlow(activity, infoTask.getResult());
                    flow.addOnCompleteListener(flowTask -> {
                        preferences.edit().putBoolean(KEY_RATING_SHOWN, true).apply();
                    });
                }
            });
        }
    }

}


text:
from utils import CSVScraper

class LincolnPersonScraper(CSVScraper):
    # https://niagaraopendata.ca/dataset/town-of-lincoln-elected-officials
    csv_url = 'https://niagaraopendata.ca/dataset/a19ecca4-b128-4779-9c42-fddb5bd5d9e8/resource/f453db0b-ba02-43d1-9e46-d0e7065862e4/download/lincolncouncil20160303.csv'
    many_posts_per_area = True


text:
using System;

namespace FactoryMethod
{
	internal class ChicagoStylePepperoniPizza : Pizza
	{
		public ChicagoStylePepperoniPizza()
		{
			Name = "Chicago Style Pepperoni Pizza";
			Dough = "Extra Thick Crust Dough";
			Sauce = "Plum Tomato Sauce";

			Toppings.Add("Shredded Mozzarella Cheese");
			Toppings.Add("Black Olives");
			Toppings.Add("Spinach");
			Toppings.Add("Eggplant");
			Toppings.Add("Sliced Pepperoni");
		}

		public override void Cut()
		{
			Console.WriteLine("Cutting the pizza into square slices");
		}
	}
}


text:
package net.paslavsky.gwt.soundmanager.client.event;

/**
 * TODO Describe class
 *
 * @author Andrey Paslavsky
 * @version 1.0
 */
public interface PlayHandler extends SMSoundEventHandler {
    void onPlay(PlayEvent event);
}


text:
package ro.pub.cs.aipi.lab04.businesslogic;

public class FormatManager extends EntityManager {

    public FormatManager() {
        table = "format";
    }

}


text:
package org.apache.metron.common.writer;

import org.apache.metron.common.configuration.writer.WriterConfiguration;

import java.io.Serializable;

public interface MessageWriter<T> extends AutoCloseable, Serializable {

  void init();
  void write(String sensorType, WriterConfiguration configurations, BulkMessage<T> message) throws Exception;
  String getName();
}


text:
Lightweight load manager for node.js-powered APIs. Executes a range of callbacks as an instance's load rises and falls. Load based on API calls, not server CPU load.

### Install

```shell
npm install load-manager
```

### Usage

As this is a lightweight module, we'll use [*restify*](https://github.com/mcavage/node-restify) for our server in preference to [*express*](https://github.com/visionmedia/express).

```coffeescript
# Require and instantiate restify and load-manager
api = require('restify').createServer()
loadManager = require('load-manager')()

# Start the server.
api.listen 8080, ->
  console.log 'listening at %s', api.url

# Load must be recorded manually.
# This can be achieved most simply as part of some middleware.
api.pre (req, res, next) ->
  loadManager.increment()

# It can also be weighted and associated with individal resources
# or methods.
api.put '/test', (req, res, next) ->
  loadManager.increment 5

averageLoad = loadmanager.getAverage()
```


text:
package c10n.testproject.java;

import c10n.C10N;
import c10n.C10NConfigBase;
import c10n.annotations.DefaultC10NAnnotations;
import c10n.annotations.En;

import java.util.Locale;

public class Main {
  public static void main(String... args) {
    /*
     * Configure C10N
     */
    C10N.configure(new C10NConfigBase() {
      @Override
      protected void configure() {
        install(new DefaultC10NAnnotations());
        bindAnnotation(En.class); //also bind En as fallback
      }
    });

    /*
     * Parse arguments
     */
    boolean showHelp = false;
    Locale locale = Locale.ENGLISH;
    String action = "none";
    for (int i = 0; i < args.length; i++) {
      String arg = args[i];
      if (arg.equals("--help")) {
        showHelp = true;
      } else if (arg.equals("--locale")) {
        i++;
        locale = parseLocale(args[i]);
      } else if (arg.equals("--action")){
        i++;
        action = args[i];
      }
    }

    //Set the locale
    Locale.setDefault(locale);

    /*
     * Act upon arguments
     */
    if(showHelp){
      System.out.println(C10N.get(Usage.class).usage());
    }else if(action.equals("action1")){
      action1();
    }else{
      System.out.println(C10N.get(Errors.class).noAction());
    }
  }

  private static void action1() {
    //TODO
  }

  private static Locale parseLocale(String localeStr) {
    String[] s = localeStr.split("_", 3);
    switch (s.length) {
      case 1:
        return new Locale(s[0]);
      case 2:
        return new Locale(s[0], s[1]);
      default:
        return new Locale(s[0], s[1], s[2]);
    }
  }
}


text:
void graph_bfs(DirGraph *graph, int start, int *result);
void graph_dfs_recur(DirGraph *graph, int start, int *result);
void graph_dfs(DirGraph *graph, int start, int *result);


text:
package com.alibaba.druid.pool.dbcp;

import java.sql.Connection;

import junit.framework.TestCase;

import org.apache.commons.dbcp.BasicDataSource;

import com.alibaba.druid.mock.MockDriver;

public class Test0 extends TestCase {

    public void test_idle() throws Exception {
        MockDriver driver = MockDriver.instance;

        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:mock:xxx");
        dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
        dataSource.setInitialSize(0);
        dataSource.setMaxActive(4);
        dataSource.setMaxIdle(4);
        dataSource.setMinIdle(1);
        dataSource.setMinEvictableIdleTimeMillis(5000 * 1);
        dataSource.setTimeBetweenEvictionRunsMillis(10);
        dataSource.setTestWhileIdle(false);
        dataSource.setTestOnBorrow(false);
        dataSource.setValidationQuery("SELECT 1");

        {
            Connection conn = dataSource.getConnection();

            // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
            System.out.println("raw size : " + driver.getConnections().size());

            conn.close();
            System.out.println("raw size : " + driver.getConnections().size());
        }

        {
            Connection conn = dataSource.getConnection();

            // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
            System.out.println("raw size : " + driver.getConnections().size());

            conn.close();
            System.out.println("raw size : " + driver.getConnections().size());
        }

        dataSource.close();
    }
}


text:
namespace AppShell
{
    public interface IWebBrowserService : IService
    {
        void Navigate(string url);
        void LoadHtml(string html);
        void LoadEmbeddedHtml(string embeddedHtml);
    }
}


text:
<!doctype html>

    <meta charset="utf-8">
    <title>Yet Another Pandoc CV Template | lf-araujo</title>
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/css/foundation.min.css">
    <link rel="stylesheet" href="/css/highlight.min.css">
    <link rel="stylesheet" href="/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/academicons.min.css">
    <link rel="stylesheet" href="/css/fonts.css">
    <link rel="stylesheet" href="/css/finite.css">

	    <button class="site-hamburger" type="button" data-toggle>

	    </button>

	      lf-araujo

		lf-araujo

Notes

Projects

Contact

Yet Another Pandoc CV Template

    <time datetime="2016-09-24 00:00:00 +0000 UTC" itemprop="datePublished">24 September 2016</time>

    pandoc

It is always nice when you come across good typography from LaTeX documents. Up until recently, the easiest way of dealing with LaTeX syntax was through the installation and use of Lyx. Fortunately, pandoc was created (it is ten years old now), making things a lot easier.

I've been using a custom CV template for some months, and finally I feel it is worth sharing to a broader audience.

Here is how it looks:

The dependencies for it are:

A working git installation;

Fira Mono & Minion Pro fonts installed in the system;

a working pandoc and xelatex installation;

and the installation of my repo (see below).

Install my repository by cloning it:

git clone https://github.com/lf-araujo/yapcvt
cd yapcvt

Now you will find a cv.md file. It starts with a yaml code block (the part between the three dashes), where you can send the desired options to pandoc. You don't have to change those, as I let them as near as possible to Butterick's Practical Typography recommendations.

Note that the items are laid out in a list of descriptions, with the following syntax:

2010-2013

:     State University of Campinas, UNICAMP, Campinas, Brazil.\newline Master's in Medical Sciences. Title: Causes and Causal Models in Psychiatry, Year of degree: 2013. Supervisor: Prof. X

Edit these accordingly and you're good to go.

The last step is to make the pdf itself, it involves the use of a Makefile. In the same directory, run make and the pdf should be rendered and opened in the proper application. If you are using MacOS, you should edit the last line of the Makefile to read open instead of xdg-open, as I don't know whether Apple follows the free desktop standards.

Acknowledgements

This cv is based on a custom version that I used up until recently, but now with strong influence from Kieran Healy's vita and from Butterick's Practical Typography pointers to a good typographical design.

      <meta itemprop="wordCount" content="317">
      <meta itemprop="datePublished" content="2016-09-24">
      <meta itemprop="url" content="/2016/09/24/yet-another-pandoc-cv-template/">

« Previous page: Changing Directions in the Design of the Text Editor

Next page: A Programming Workflow for the Genie Programming Language »

     Do you like this, or do you have a comment? Then please follow me at @lf_ar on  Twitter!


text:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Orleans.Runtime;

namespace Orleans.Streams
{
    internal class GrainBasedPubSubRuntime : IStreamPubSub
    {
        public Task<ISet<PubSubSubscriptionState>> RegisterProducer(StreamId streamId, string streamProvider, IStreamProducerExtension streamProducer)
        {
            var streamRendezvous = GetRendezvousGrain(streamId);
            return streamRendezvous.RegisterProducer(streamId, streamProducer);
        }

        public Task UnregisterProducer(StreamId streamId, string streamProvider, IStreamProducerExtension streamProducer)
        {
            var streamRendezvous = GetRendezvousGrain(streamId);
            return streamRendezvous.UnregisterProducer(streamId, streamProducer);
        }

        public Task RegisterConsumer(GuidId subscriptionId, StreamId streamId, string streamProvider, IStreamConsumerExtension streamConsumer, StreamSequenceToken token, IStreamFilterPredicateWrapper filter)
        {
            var streamRendezvous = GetRendezvousGrain(streamId);
            return streamRendezvous.RegisterConsumer(subscriptionId, streamId, streamConsumer, token, filter);
        }

        public Task UnregisterConsumer(GuidId subscriptionId, StreamId streamId, string streamProvider)
        {
            var streamRendezvous = GetRendezvousGrain(streamId);
            return streamRendezvous.UnregisterConsumer(subscriptionId, streamId);
        }

        public Task<int> ProducerCount(Guid guidId, string streamProvider, string streamNamespace)
        {
            StreamId streamId = StreamId.GetStreamId(guidId, streamProvider, streamNamespace);
            var streamRendezvous = GetRendezvousGrain(streamId);
            return streamRendezvous.ProducerCount(streamId);
        }

        public Task<int> ConsumerCount(Guid guidId, string streamProvider, string streamNamespace)
        {
            StreamId streamId = StreamId.GetStreamId(guidId, streamProvider, streamNamespace);
            var streamRendezvous = GetRendezvousGrain(streamId);
            return streamRendezvous.ConsumerCount(streamId);
        }

        public Task<List<GuidId>> GetAllSubscriptions(StreamId streamId, IStreamConsumerExtension streamConsumer)
        {
            var streamRendezvous = GetRendezvousGrain(streamId);
            return streamRendezvous.GetAllSubscriptions(streamId, streamConsumer);
        }

        private static IPubSubRendezvousGrain GetRendezvousGrain(StreamId streamId)
        {
            return (IPubSubRendezvousGrain)GrainClient.InvokeStaticMethodThroughReflection(
                "Orleans",
                "Orleans.Streams.PubSubRendezvousGrainFactory",
                "GetGrain",
                new Type[] { typeof(Guid), typeof(string) },
                new object[] { streamId.Guid, streamId.ProviderName + "_" + streamId.Namespace });
        }

        public GuidId CreateSubscriptionId(IAddressable requesterAddress, StreamId streamId)
        {
            return GuidId.GetNewGuidId();
        }

    }

    internal class StreamPubSubImpl : IStreamPubSub
    {
        private readonly IStreamPubSub explicitPubSub;
        private readonly ImplicitStreamSubscriberTable implicitPubSub;

        public StreamPubSubImpl(IStreamPubSub explicitPubSub, ImplicitStreamSubscriberTable implicitPubSub)
        {
            if (explicitPubSub == null)
            {
                throw new ArgumentNullException("explicitPubSub");
            }

            if (implicitPubSub == null)
            {
                throw new ArgumentNullException("implicitPubSub");
            }

            this.explicitPubSub = explicitPubSub;
            this.implicitPubSub = implicitPubSub;
        }

        public async Task<ISet<PubSubSubscriptionState>> RegisterProducer(StreamId streamId, string streamProvider, IStreamProducerExtension streamProducer)
        {
            ISet<PubSubSubscriptionState> result = await explicitPubSub.RegisterProducer(streamId, streamProvider, streamProducer);
            if (String.IsNullOrWhiteSpace(streamId.Namespace)) return result;

            ISet<IStreamConsumerExtension> implicitSet = implicitPubSub.GetImplicitSubscribers(streamId);
            foreach (var consumer in implicitSet)
            {
                // we ignore duplicate entries-- there's no way a programmer could prevent the duplicate entry from being added if we threw an exception to communicate the problem.
                GuidId subscriptionId = GuidId.GetGuidId(GrainExtensions.GetGrainId(consumer).GetPrimaryKey());
                result.Add(new PubSubSubscriptionState(subscriptionId, streamId, consumer, null, null));
            }
            return result;
        }

        public Task UnregisterProducer(StreamId streamId, string streamProvider, IStreamProducerExtension streamProducer)
        {
            return IsImplicitSubscriber(streamProducer, streamId) ? TaskDone.Done :
                explicitPubSub.UnregisterProducer(streamId, streamProvider, streamProducer);
        }

        public Task RegisterConsumer(GuidId subscriptionId, StreamId streamId, string streamProvider, IStreamConsumerExtension streamConsumer, StreamSequenceToken token, IStreamFilterPredicateWrapper filter)
        {
            return IsImplicitSubscriber(streamConsumer, streamId)
                ? TaskDone.Done
                : explicitPubSub.RegisterConsumer(subscriptionId, streamId, streamProvider, streamConsumer, token, filter);
        }

        public Task UnregisterConsumer(GuidId subscriptionId, StreamId streamId, string streamProvider)
        {
            return IsImplicitSubscriber(subscriptionId, streamId)
                ? TaskDone.Done
                : explicitPubSub.UnregisterConsumer(subscriptionId, streamId, streamProvider);
        }

        public Task<int> ProducerCount(Guid streamId, string streamProvider, string streamNamespace)
        {
            return explicitPubSub.ProducerCount(streamId, streamProvider, streamNamespace);
        }

        public Task<int> ConsumerCount(Guid streamId, string streamProvider, string streamNamespace)
        {
            return explicitPubSub.ConsumerCount(streamId, streamProvider, streamNamespace);
        }

        public async Task<List<GuidId>> GetAllSubscriptions(StreamId streamId, IStreamConsumerExtension streamConsumer)
        {
            return IsImplicitSubscriber(streamConsumer, streamId)
                ? new List<GuidId>( new [] { GuidId.GetGuidId(streamConsumer.GetPrimaryKey()) } )
                : await explicitPubSub.GetAllSubscriptions(streamId, streamConsumer);
        }

        private bool IsImplicitSubscriber(IAddressable addressable, StreamId streamId)
        {
            return implicitPubSub.IsImplicitSubscriber(GrainExtensions.GetGrainId(addressable), streamId);
        }
        private bool IsImplicitSubscriber(GuidId subscriptionId, StreamId streamId)
        {
            return implicitPubSub.IsImplicitSubscriber(subscriptionId, streamId);
        }

        public GuidId CreateSubscriptionId(IAddressable requesterAddress, StreamId streamId)
        {
            GrainId grainId = GrainExtensions.GetGrainId(requesterAddress);
            // If there is an implicit subscription setup for the provided grain on this provided stream, subscription should match the stream Id.
            // If there is no implicit subscription setup, generate new random unique subscriptionId.
            // TODO: Replace subscription id with statically generated subscriptionId instead of getting it from the streamId.
            return implicitPubSub.IsImplicitSubscriber(grainId, streamId)
                ? GuidId.GetGuidId(grainId.GetPrimaryKey())
                : GuidId.GetNewGuidId();
        }
    }
}


text:
using SharepointCommon.Test.Entity;

namespace SharepointCommon.Test.Repository
{
    public class TestRepository : ListBase<OneMoreField<string>>
    {
        public override void Add(OneMoreField<string> entity)
        {
            entity.AdditionalField = "overriden!";
            base.Add(entity);
        }
    }

    public class TestRepositoryInheritedTwice : TestRepository
    {
    }
}


text:
package beforeApril.firstDay.ForestAcademy.week2;

/**
 * Created by Alex_Xie on 25/02/2017.
 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

 For example:
 Given the below binary tree and sum = 22,
 5
 / \
 4   8
 /   / \
 11  13  4
 /  \      \
 7    2      1
 return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

 https://leetcode.com/problems/path-sum/?tab=Description
 */
public class HasPathSum {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public class Solution {
        public boolean hasPathSum(TreeNode root, int sum) {

            if (root == null) return false;
            if (root.left == null && root.right == null && sum - root.val == 0){
                return true;
            }

            return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val);

        }

    }
}


text:
let links = [
{
  link : "https://goo.gl/2idTtW"
}
];

module.exports = {
  commandAlias : "simple",
  getDownloadLinks : getDownloadLinks,
  links : links
};

function getDownloadLinks(callback) {
    return callback(null, "link", links);
}