public static void initOffsets(final PsiFile file, final OffsetMap offsetMap) {
    int offset =
        Math.max(
            offsetMap.getOffset(CompletionInitializationContext.SELECTION_END_OFFSET),
            offsetMap.getOffset(CompletionInitializationContext.IDENTIFIER_END_OFFSET));

    PsiElement element = file.findElementAt(offset);
    if (element instanceof PsiWhiteSpace
        && (!element.textContains('\n')
            || CodeStyleSettingsManager.getSettings(file.getProject())
                .getCommonSettings(JavaLanguage.INSTANCE)
                .METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE)) {
      element = file.findElementAt(element.getTextRange().getEndOffset());
    }
    if (element == null) return;

    if (LEFT_PAREN.accepts(element)) {
      offsetMap.addOffset(LPAREN_OFFSET, element.getTextRange().getStartOffset());
      PsiElement list = element.getParent();
      PsiElement last = list.getLastChild();
      if (last instanceof PsiJavaToken
          && ((PsiJavaToken) last).getTokenType() == JavaTokenType.RPARENTH) {
        offsetMap.addOffset(RPAREN_OFFSET, last.getTextRange().getStartOffset());
      }

      offsetMap.addOffset(ARG_LIST_END_OFFSET, list.getTextRange().getEndOffset());
    }
  }
 private static OffsetMap translateOffsetMapToInjected(
     OffsetMap hostMap, DocumentWindow injectedDocument) {
   final OffsetMap map = new OffsetMap(injectedDocument);
   for (final OffsetKey key : hostMap.getAllOffsets()) {
     map.addOffset(key, injectedDocument.hostToInjected(hostMap.getOffset(key)));
   }
   return map;
 }
 private static OffsetMap translateOffsetMapToHost(
     CompletionInitializationContext initContext,
     PsiFile context,
     PsiFile hostFile,
     Editor hostEditor) {
   final InjectedLanguageManager injectedLanguageManager =
       InjectedLanguageManager.getInstance(hostFile.getProject());
   final OffsetMap hostMap = new OffsetMap(hostEditor.getDocument());
   final OffsetMap original = initContext.getOffsetMap();
   for (final OffsetKey key : original.getAllOffsets()) {
     hostMap.addOffset(
         key, injectedLanguageManager.injectedToHost(context, original.getOffset(key)));
   }
   return hostMap;
 }